iOS SDK:在AppDelegate中移动文件

时间:2011-06-07 11:06:19

标签: iphone ios file copy nsfilemanager

我用IB创建了我的笔尖和子视图......

现在在我的AppDelegate中我得到了这段代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        [self initDatabase];

        // Override point for customization after application launch.
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];

        [self.window makeKeyAndVisible];
        [self.window addSubview:tabBar.view];

        [Appirater appLaunched:YES];

        return YES;
    }

其中[self initDatabase]是这样的:

- (BOOL)copyDB {
    /* Database */
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentFolderPath = [searchPaths objectAtIndex:0];
    NSString *dbFilePath = [documentFolderPath stringByAppendingPathComponent:@"food.db"];

    NSError *error;

    if(![[NSFileManager defaultManager] fileExistsAtPath:dbFilePath]) {
        // copy db
        NSString *backupDbPath = [Food getDBFileBundle];
        NSLog(@"Bundle DB Path: %@", backupDbPath);
        if(backupDbPath == nil) {
            NSLog(@"Unable to find main DB");
            return NO;
        } else {
            BOOL copiedBackupDb = [[NSFileManager defaultManager] copyItemAtPath:backupDbPath toPath:dbFilePath error:&error];
            if(!copiedBackupDb) {
                NSLog(@"Unable to copy main DB");
                NSLog(@"Error description: %@", [error localizedDescription]);
                return NO;
            } else {
                NSLog(@"DB Copied in %@", dbFilePath);
                return YES;
            }
        }
    }
    else {
        NSLog(@"Local DB already exists at path: %@", dbFilePath);
        return NO;
    }

    return YES;
}

现在每次在模拟器或设备上启动时,在调试器中我看到本地数据库已经存在于路径中:bla bla bla ...它不应该存在! (文件大小也等于零!)

现在,如果我在tabbar中的第一个视图控制器中移动initDatabase过程(在viewDidLoad方法中),它就像一个魅力......

怎么回事?

我已经清理了项目,在我尝试之前在模拟器和设备上删除了应用程序...发生了什么?我该如何解决这个问题?

我正在使用带有4.x固件的Xcode 4.0.2

0 个答案:

没有答案