2012-09-10 40 views
4

對於每個應用更新,我想完全擦除我的整個Core Data數據庫,然後重新設置它。我沒有成功這樣做。我嘗試了各種各樣的東西,這似乎是最接近我來的。我發現了幾個SO帖子,但沒有一個解決方案適用於我的目的。MagicalRecord:刪除整個數據並重新設置核心數據堆棧

我使用的是MagicalRecord,它提供了幾種簡短的手段來獲取各種對象。這裏是我的代碼:

if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"buildVersion"] intValue] < [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] intValue]) { 
    NSPersistentStore *store = [NSPersistentStore defaultPersistentStore]; 
    NSError *error; 
    NSURL *storeURL = [NSPersistentStore defaultLocalStoreUrl]; 
    NSPersistentStoreCoordinator *storeCoordinator = [NSPersistentStoreCoordinator defaultStoreCoordinator]; 
    [storeCoordinator removePersistentStore:store error:&error]; 
    [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error]; 
    [[NSUserDefaults standardUserDefaults] setObject:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] forKey:@"buildVersion"]; 
} 

[MagicalRecord setupCoreDataStack]; 

回答

4

有一個NSPersistentStore+MagicalRecord類別,它可以讓你獲得持久存儲的網址:

+ (NSURL *) MR_urlForStoreName:(NSString *)storeFileName; 

您可以使用URL從這個調用刪除的持久性存儲的SQLite數據庫使用NSFileManager。