【问题标题】:Migrate Persistant Store Crash迁移持久存储崩溃
【发布时间】:2014-05-11 04:39:06
【问题描述】:

我有一个开关可以为 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

知道为什么会这样吗?这有点奇怪,因为在我的其他设备之一上,它此时并没有崩溃。

【问题讨论】:

  • 你为什么弱引用你的psc对象?
  • 我更改了 WEAK 引用,但仍然出现崩溃。

标签: ios objective-c core-data icloud core-data-migration


【解决方案1】:

试试这个

[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

【讨论】:

  • 这不会删除我正在尝试迁移的持久存储吗?
  • 这也没有解决。仍然崩溃
  • @AmitWadhawan,我更新了答案。尝试新的解决方案。对我来说很好用
  • 谢谢!会试一试,让你知道。
猜你喜欢
  • 2014-11-28
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多