我有一个使用Core Data的iPad应用程序.
我正在尝试使用sqlite文件的Dropbox同步来为我的用户提供另一种备份方式.
从Dropbox备份和恢复是没有问题的;但是,一旦恢复,必须重置应用程序以显示数据.
我假设这是因为持久存储仍在使用旧文件.
如何强制关闭旧模型并重新打开?
我试过以下没有运气:
NSPersistentStore* store = [[_persistentStoreCoordinator persistentStores] lastObject];
[_persistentStoreCoordinator removePersistentStore:store error:nil];
[__managedObjectModel release];
__managedObjectModel = nil;
[_persistentStoreCoordinator release];
_persistentStoreCoordinator = nil;
[self persistentStoreCoordinator];
[self managedObjectContext];
[self managedObjectModel];
最佳答案 最后,我使用以下内容,重新加载持久存储:
NSError *error;
// retrieve the store URL
NSURL * storeURL = [[__managedObjectContext persistentStoreCoordinator] URLForPersistentStore:[[[__managedObjectContext persistentStoreCoordinator] persistentStores] lastObject]];
// lock the current context
[__managedObjectContext lock];
[__managedObjectContext reset];//to drop pending changes
//delete the store from the current managedObjectContext
if ([[__managedObjectContext persistentStoreCoordinator] removePersistentStore:[[[__managedObjectContext persistentStoreCoordinator] persistentStores] lastObject] error:&error])
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
[[__managedObjectContext persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error];//recreates the persistent store
}
dispatch_async(dispatch_get_main_queue(), ^{
self.ready = YES;
[[NSNotificationCenter defaultCenter]postNotificationName:kModelDidChange object:nil];
});
[__managedObjectContext unlock];