迁移Core Data存储时出现错误消息 [英] Error message when migrating Core Data store
问题描述
问题:
我有一些很简单的代码> __ NSArrayM replaceObjectAtIndex:withObject:
]:object不能为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);
}
}
更多信息:
- 如果没有数据,则不会发生异常。而是相同的函数设置一个错误,用userInfo无法添加相同的存储两次。
- 我目前正在做一个小的简单项目
找到了it!
显然,当您的iCloud网址和本地网址相同时,Core Data不喜欢迁移。我认为这不应该重要,因为真的,iCloud数据存储在自己的目录。但是,似乎在迁移期间,使用相同的名称会导致问题。 BAZINGA!
轻松修复 - 只需这样做:
- (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];
}
}
The Problem:
I've got some pretty simple code (below) that causes an exception during 'migratePersistentStore
' with the error message
Exception:*** -[__NSArrayM replaceObjectAtIndex:withObject:]: object cannot be nil
The Code:
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);
}
}
Further Info:
- It appears that if no data is present, the exception does not occur. Instead the same function sets an error, with userInfo "Can't add the same store twice".
- I'm currently making a small simple project to try and replicate the problem, I'll post a link here once it's done.
Found it!
Apparently, Core Data doesn't like migration when your iCloud URL and local URL are the same. I thought this shouldn't matter because really, iCloud data is stored in its own directory. BUT, it seems during migration, using the same name causes problems. BAZINGA!
Easy fix - just do something like this:
- (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"];
}
}
这篇关于迁移Core Data存储时出现错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!