迁移持久存储崩溃 [英] Migrate Persistant Store Crash
问题描述
我有一个开关,为iOS 7应用程序打开或关闭iCloud。 iCloud同步工作正常。当iCloud打开并关闭时,我调用以下代码:
I have a switch that turns iCloud on or off for an iOS 7 app. iCloud sync works fine. When iCloud is on and I turn it off, I call this code:
- (void)migrateiCloudStoreToLocalStore {
NSError *error;
__weak NSPersistentStoreCoordinator *psc = self.managedObjectContext.persistentStoreCoordinator;
NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption:@YES,
NSPersistentStoreRemoveUbiquitousMetadataOption : @YES};
NSPersistentStore *currentStore = [psc persistentStores][0];
NSLog(@"iCloud Store - %@", currentStore);
NSLog(@"Local Store - %@", self.store);
[psc migratePersistentStore:currentStore
toURL:self.store
options:options
withType:NSSQLiteStoreType
error:&error];
}
商店确实存在,但是仍然会在 migratePersistentStore:toURL:options:withType:error:
时调用。
I can see that both stores do exist but it still crashes when migratePersistentStore:toURL:options:withType:error:
is called.
错误我得到:
***由于未捕获异常NSInvalidArgumentException终止应用程序,原因:'*** - [__ NSArrayM replaceObjectAtIndex: withObject:]:对象不能是零'
***第一掷调用堆栈:
(0x2e0a3fd3 0x38850ccf 0x2dfdf29f 0x2de9e7f7 0xa9b61 0xb2305 0xb019f 0x309de05f 0x30a90377 0x3093f6f5 0x308b855b 0x2e06f2a5 0x2e06cc49 0x2e06cf8b 0x2dfd7f4f 0x2dfd7d33 0x32edc663 0x3092316d 0x30289 0x38d5dab7)
libc ++ abi.dylib:以NSException类型的未捕获异常终止
任何想法为什么会发生这种情况?
Any idea why this is happening? It's a bit odd because on one of my other devices, it doesn't crash at this point.
推荐答案
试试这个
[psc removePersistentStore:currentStore error:nil];
[psc migratePersistentStore:currentStore
toURL:self.store
options:options
withType:NSSQLiteStoreType
error:&error];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:self.store
options:options /* options for local store */
error:nil];
UPDATE:新解决方案
NSPersistentStoreCoordinator * persistentStoreCoordinator = self.persistentStoreCoordinator;
NSPersistentStore * persistentStore = [[persistentStoreCoordinator persistentStores] firstObject];
NSMutableDictionary * localStoreOptions = [[self localPersistentStoreOptions] mutableCopy];
[localStoreOptions setObject:@YES forKey:NSPersistentStoreRemoveUbiquitousMetadataOption];
[[NSFileManager defaultManager] copyItemAtPath:persistentStore.URL.path toPath:[self storeURL].path error:nil];
NSPersistentStoreCoordinator * newPersistentStoreCoordinator;
newPersistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
[newPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:[self storeURL]
options:localStoreOptions
error:nil];
[self setPersistentStoreCoordinator:newPersistentStoreCoordinator];
[self setupManagedObjectContext]; //initialize new moc with new persistent store coordinator
这篇关于迁移持久存储崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!