当用户升级或重新安装较新版本的iOS应用程序时,是否会调用任何委托方法?
我使用Core Data从服务器缓存一些信息。当任何实体的模式发生变化时,我需要从模拟器中手动删除SQLite数据库,否则应用程序将在启动时崩溃,并显示错误“用于打开存储的模型与用于创建存储的模型不兼容“。如果有任何代理方法进行应用升级,则可以自动删除。
答案 0 :(得分:4)
您需要使用核心数据版本控制:
答案 1 :(得分:2)
有时很高兴了解上述内容。确保在设置标记后立即保存默认值并执行所需的任何版本控制,因此崩溃不会让您再次执行此操作。
编辑:如果模型更改,如何不崩溃。我现在使用它,保留旧的存储库,并调整模型,在每次调整时它只删除旧的(如果它无法打开它)并创建一个新的。这是以Apple的代码为蓝本的,但不确定我做了哪些更改。在任何情况下,如果模型发生变化,您都不会崩溃。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
//LTLog(@"_persistentStoreCoordinator = %@", _persistentStoreCoordinator);
if (_persistentStoreCoordinator)
{
return _persistentStoreCoordinator;
}
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = [[appDelegate applicationAppSupportDirectory] stringByAppendingPathComponent:[_dbName stringByAppendingPathExtension:@"SQLite"]];
storeURL = [NSURL fileURLWithPath:path];
BOOL fileExists = [manager fileExistsAtPath:path];
if(!fileExists) {
_didCreateNewRepository = YES;
}
if(_createNewRepository) {
[manager removeItemAtURL:storeURL error:nil];
if(fileExists) _didDestroyOldRepository = YES;
_didCreateNewRepository = YES;
}
while(YES) {
__autoreleasing NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if ([_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
break;
} else {
_persistentStoreCoordinator = nil;
[manager removeItemAtURL:storeURL error:&error];
if(fileExists) {
_didDestroyOldRepository = YES; // caller didn't want a new one but got a new one anyway (old one corrupt???)
_didCreateNewRepository = YES;
}
#ifndef NDEBUG
LTLog(@"CORE DATA failed to open store %@: error=%@", _dbName, error);
#endif
/*
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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object model
Check the error message to determine what the actual problem was.
*/
//LTLog(@"Unresolved error %@, %@", error, [error userInfo]);
//abort();
}
}
return _persistentStoreCoordinator;
}
答案 2 :(得分:1)