2011-11-21 53 views
0

作为我的应用程序启动的一部分,我将包文件复制到我的文档目录。从主包复制创建文件大小为零kb

这对我的四个文件中的三个很好,但第四个创建一个零KB文件。

在iOS 5.0 sim上运行。我已经清理了几次这个版本,并检查了文件名的大写字母是否正确。

的文件出现在目录中,但零KB,应该是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]); 
    } 
} 
+1

包含有问题的代码,所以我们可以看到的部分是什么你做错了。 – WrightsCS

+0

包含代码一件事日志给我一个1(即该文件存在于路径位置,我称之为\t dbname = @“UserProfile.db”; \t if(![self CheckDBs:dbname]){//检查User.db是否存在于Documents目录中 \t \t [self copyDBs:dbname]; //拷贝捆绑的数据库 \t} –

回答

0

你是否还检查文件的大小?

尝试重置您的模拟器。从NSFileManager文档:

如果具有相同名称的文件dstPath已经存在,这种方法 中止复制的尝试,并返回相应的错误。

确保目的地是空的,然后重试。另外,请检查error对象。

如果所有检查出来的文件名都是,则拼写为。检查包中是否存在确切文件,使用文件名或路径的NSLog等。您应该找到该错误。还要检查Finder中的相应文件夹。

而不是使用

[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname] 

的尝试

[[NSBundle mainBundle] pathForResource:shortName ofType:@"db"] 
+0

是的,当我在finder中选择显示包内容时,它显示资源是24kb。然后当我检查文档文件夹时,文件显示为零kb。 –

+0

没有快乐......模拟复位没有区别 –

+0

“错误”是什么意思? – Mundi

0

好吧,我想通了,是什么原因造成的问题。

当我运行应用程序时,appdidfinishlaunching方法在其中一个视图控制器加载之前未完成。该视图控制器尝试访问从该包复制的其中一个文件。

我猜sqlite创建文件时,当您尝试访问数据库,它创建与零字节长度。

所以当我的appdidfinish启动方法检查文件的存在时,由于sql调用而存在。

这通常只会在应用程序首次运行之前发生问题,因为之后数据库将存在。现在

的问题是如何获得的appdidfinish启动完成其余之前被允许开始所讨论的视图控制器的MainWindow.xib中

+0

好吧我移动在添加tabbarcontroller之前使用firstrun方法,并在启动viewcontroller加载方法之前完成此操作。 –