ios - 迁移核心数据存储时的错误消息

标签 ios iphone objective-c ipad core-data

问题:

我有一些非常简单的代码(如下)在“migratePersistentStore”期间导致异常并显示错误消息

Exception:*** -[__NSArrayM replaceObjectAtIndex:withObject:]: object cannot be nil


代码:

NSPersistentStore * oldStore = [_persistentStoreCoordinator persistentStores][0];

if (oldStore) {

    @try {

        [_persistentStoreCoordinator migratePersistentStore:oldStore 
              toURL:[self storeURL] 
              options: @{ NSPersistentStoreRemoveUbiquitousMetadataOption : @YES } 
              withType:NSSQLiteStoreType error:&error];

    }
    @catch(NSException* ex) {

        NSLog(@"Exception:%@", ex.description);

    }

}


更多信息:

  • 看来,如果没有数据存在,则不会发生异常。相反,相同的函数会设置错误,用户信息为“无法添加同一商店两次”。
  • 我目前正在做一个简单的小项目来尝试重现这个问题,完成后我会在此处发布一个链接。

最佳答案

显然,当您的 iCloud URL 和本地 URL 相同时,Core Data 不喜欢迁移。我认为这应该无关紧要,因为实际上,iCloud 数据存储在它自己的目录中。但是,似乎在迁移过程中,使用相同的名称会导致问题。

简单的修复 - 只需做这样的事情:

- (NSURL *)storeURL {

    NSURL * documentsDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];

    if (iCloudEnabled]) {
        return [documentsDirectory URLByAppendingPathComponent:@"iCloudEventData.sqlite"];
    } else {
        return [documentsDirectory URLByAppendingPathComponent:@"EventData.sqlite"];
    }

}

关于ios - 迁移核心数据存储时的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22876784/

相关文章:

ios - 通知回调中的 sleep 功能未按预期工作

ios - 文件 URI 和文件路径 : phonegap plugin capture and camera

ios - 如何在 iPhone 锁定屏幕上放置一个图标?

iphone - xcode将目标从“iOS设备”更改为模拟器

IOS swift - objective-c 代码迁移到 swift

objective-c - 如何使用 block 作为参数

ios - swift 错误 : value of optional type 'Double?' not unwrapped

iphone - 如何使应用程序支持 ios6 和 iphone 5

ios - 如何在 iOS 上使用 GSEvent 模拟触摸移动事件?

objective-c - Objective-C ,NSMutableDictionary 引用计数