2014-03-31 119 views
1

我有一个开关可以打开或关闭iOS 7应用程序的iCloud。 iCloud同步工作正常。当iCloud的是,我把它关掉,我把这个代码:迁移持久性商店崩溃

- (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:被调用。

这里的错误,我得到:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM replaceObjectAtIndex:withObject:]: object cannot be nil' *** First throw call stack: (0x2e0a3fd3 0x38850ccf 0x2dfdf29f 0x2de9e7f7 0xa9b61 0xb2305 0xb019f 0x309de05f 0x30a90377 0x3093f6f5 0x308b855b 0x2e06f2a5 0x2e06cc49 0x2e06cf8b 0x2dfd7f4f 0x2dfd7d33 0x32edc663 0x3092316d 0x30289 0x38d5dab7) libc++abi.dylib: terminating with uncaught exception of type NSException

任何想法,为什么发生这种情况?这有点奇怪,因为在我的其他设备之一,它在这一点上不会崩溃。

+0

你为什么弱引用您的'psc'对象? – ismailgulek

+0

我改变了这个弱引用,但仍然崩溃。 – Amit

回答

0

尝试这个

[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]; 

更新:新的解决方案

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 
+0

难道不会删除我尝试迁移的持久性商店吗? – Amit

+0

@AmitWadhawan,我编辑答案。 – vitkuzmenko

+0

这也没有解决它。仍然收到崩溃 – Amit