2010-03-02 105 views
0

我正在使用coredata在我的第一个应用程序。一切都很完美。当我在我的设备上调试应用程序(没有它已经安装在设备上)时,然后退出应用程序,手动修改sqlite dbase然后再次调试应用程序,它似乎使用旧版本的数据库。如果两个sqlite文件具有相同的名称,是否有办法告诉它只是替换那里?CoreData更新的SQLite数据库

下面是我的代码片段:

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"mydata.sqlite"]; 

    /* 
    Set up the store. 
    For the sake of illustration, provide a pre-populated default store. 
    */ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    // If the expected store doesn't exist, copy the default store. 
    if (![fileManager fileExistsAtPath:storePath]) { 
     NSLog(@"copying default from sqlite"); 
     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"mydata" ofType:@"sqlite"]; 
     if (defaultStorePath) { 
      [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
     } 
    } 


    NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 

正如你可以看到,如果没有最初找到数据库从应用程序加载sqlite的文件。除了更改sqlite文件的名称或使用设置,有没有办法说如果你有什么可以刷新你所拥有的,并从头开始?

我担心用户购买应用程序,然后当我发布更新时,他们仍然看到来自旧版数据库的数据。

感谢, 豪伊

+0

“......有没有办法说清楚你有什么需要刷新的东西,然后从头开始?”如果这个商店是只读的,用户必须为他们自己创建一个空白的自己的数据,那没问题。否则,这是一个非常糟糕的主意。 – 2010-03-02 10:29:02

回答

0

您可以删除旧的数据库,并复制一个附带您的更新以同样的方式你做到了最初。

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"Test.sqlite"]; 
NSURL *storeURL = [NSURL fileURLWithPath:storePath]; 
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] 

您只需要一些方法来确定数据库是否过时。