从主包复制创建的文件大小为零 kb
作为我的应用程序启动的一部分,我将捆绑文件复制到我的文档目录中。
这对于我的四个文件中的三个来说效果很好,但第四个文件创建了一个零 KB 文件。
在 iOS 5.0 sim 上运行。我已经清理了几次构建并检查了文件名大小写是否正确。
该文件出现在目录中,但大小为零,
如果有任何帮助,应该是 24K。
-(BOOL) CheckDBs: (NSString *)dbname
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath: dbPath];
NSLog(@"AppDelegate CheckDatabase: %@ = %i", dbPath, success);
if (success) {
//NSLog(@"return YES");
return YES;
}
else {
return NO;
}
} // Complete - checks if files exist in the User Documents directory
-(void) copyDBs: (NSString *) dbname
{
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname];
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (success) {
// Version 4.0 code
//NSDictionary *attribs = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
//success = [fileManager setAttributes:attribs ofItemAtPath:dbPath error:&error];
NSLog(@"AppDelegate copyDatase: %@ = %d", dbPath, success);
}
//NSLog(@"AppDelegate copyDatase: %@ = %d", dbPath, success);
if (!success) {
NSLog(@"Failed to copy database: '%@'", [error localizedDescription]);
// NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
As part of my app start-up i copy bundle files to my documents directory.
This works fine for three out of four of my files but the fourth one create a Zero KB file.
running on iOS 5.0 sim. I have cleaned the build several times and checked the file name capitalization vis correct.
the file appears in the directory but is zero kb and should be 24K
any help appreciated.
-(BOOL) CheckDBs: (NSString *)dbname
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath: dbPath];
NSLog(@"AppDelegate CheckDatabase: %@ = %i", dbPath, success);
if (success) {
//NSLog(@"return YES");
return YES;
}
else {
return NO;
}
} // Complete - checks if files exist in the User Documents directory
-(void) copyDBs: (NSString *) dbname
{
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname];
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (success) {
// Version 4.0 code
//NSDictionary *attribs = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
//success = [fileManager setAttributes:attribs ofItemAtPath:dbPath error:&error];
NSLog(@"AppDelegate copyDatase: %@ = %d", dbPath, success);
}
//NSLog(@"AppDelegate copyDatase: %@ = %d", dbPath, success);
if (!success) {
NSLog(@"Failed to copy database: '%@'", [error localizedDescription]);
// NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否还检查过原始文件大小?
尝试重置您的模拟器。来自 NSFileManager 文档:
确保目的地为空,然后重试。另外,检查
error
对象。如果所有检查结果都正确,则文件名拼写一定有错误。检查捆绑包、NSLog 中是否存在确切的文件(无论您使用文件名或路径等)。您应该会发现错误。另请检查 Finder 中的相应文件夹。
而不是使用
尝试
Have you also checked the original file size?
Try resetting your simulator. From the
NSFileManager
documentation:Make sure the destination is empty and try again. Also, check the
error
object.If all that checks out there has got to be an error in spelling the file name. Check if the exact file exists in bundle, NSLog wherever you use a file name or path, etc. You should find the error. Also check the appropriate folder in the Finder.
Instead of using
try
好吧,我弄清楚是什么导致了这个问题。
当我运行应用程序时,appdidfinishlaunching 方法在加载视图控制器之一之前尚未完成。该视图控制器尝试访问从捆绑包复制过来的文件之一。
我猜测当您尝试访问数据库时 sqlite 会创建该文件,它会以零字节长度创建该文件。
因此,当我的 appdidfinish 启动方法检查文件是否存在时,该文件由于 sql 调用而存在。
这通常只会在首次运行应用程序之前成为问题,因为之后数据库将存在。
现在的问题是我如何让 appdidfinish 启动在其余部分允许启动之前完成,因为有问题的视图控制器是 mainwindow.xib 的一部分
Ok I figured out what is causing the problem.
as i run the app the appdidfinishlaunching method is not complete before one of the view controllers is loading. That view controller attempts to access one of the files being copied over from the bundle.
I'm guessing that sqlite creates the file when you attempt to access the database, it creates it with with a zero bytes length.
So when my appdidfinish launching method checks for the existance of the file it exists due to the sql call.
This is usually only going to be a problem prior to the first run of the app as after that the database will exist.
problem now is how do i get the appdidfinish launching to complete prior to the rest being allow to start as the view controller in question is part of the mainwindow.xib