2014-11-25 163 views
0

我开发了一个iOS应用程序。该应用程序使用CoreData,我可能会在未来更新CoreData上的某些内容。CoreData防止更新崩溃

我知道如果CoreData发生任何更改,应用程序将因为更改而崩溃(在具有旧版本的设备上),并在AppDelegate上处理。

为了使应用程序不会崩溃,我认为我需要对下列方法的一些变化:有以下注释行

func saveContext() 
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? 

这些方法调用abort()功能(默认):

// Replace this implementation with code to handle the error appropriately. 
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 

在发布应用程序之前我应该​​怎么做才能避免下次更新时发生崩溃?

回答

1

最简单的方法是创建模型的新版本:当您已经做出的地方,你也可以在此改变peristentStoreCoordinator方法破坏数据库,并在情况下重新创建

http://www.raywenderlich.com/27657/how-to-perform-a-lightweight-core-data-migration

的极端糟糕的情况(用户丢失了所有数据,但应用程序开始)通过添加下面的代码(objc)而不是仅仅中止它会破坏数据库,并创建一个空的。它可以在开发中有用,这应该是prod的一个保障,但是你的代码不应该去那里。

//delete the store 
[[NSFileManager defaultManager] removeItemAtPath:storePath error:nil]; 
// recreate the store 
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
}