2010-03-02 49 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] 

您只需要一些方法來確定數據庫是否過時。