Core Data - 无法在 iOS 5.1 上创建持久存储
Posted
技术标签:
【中文标题】Core Data - 无法在 iOS 5.1 上创建持久存储【英文标题】:Core Data - persistent store can not be created on iOS 5.1 【发布时间】:2012-07-15 14:17:48 【问题描述】:当我想创建持久存储时出现错误。 当我为 ios 5.0 构建时,没有错误并且核心数据成功运行。
这是创建持久存储的代码:
+ (NSManagedObjectContext *)getContext
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES],
NSInferMappingModelAutomaticallyOption, nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSURL *storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingFormat:@"Database.sqlite"]];
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]];
NSError *error = nil;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
NSLog(@"error loading persistent store..");
[[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
//abort();
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:persistentStoreCoordinator];
return context;
错误日志:
error loading persistent store..
2012-07-15 13:16:31.440 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xde88ca0 reason=Failed to create file; code = 1,
reason = "Failed to create file; code = 1";
2012-07-15 13:16:31.455 Partyfinder[8213:707] error loading persistent store..
2012-07-15 13:16:31.458 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xdeb5360 reason=Failed to create file; code = 1,
reason = "Failed to create file; code = 1";
2012-07-15 13:16:31.467 Partyfinder[8213:707] error loading persistent store..
2012-07-15 13:16:31.470 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xdeb6680 reason=Failed to create file; code = 1,
reason = "Failed to create file; code = 1";
2012-07-15 13:16:31.483 Partyfinder[8213:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'
*** First throw call stack:
(0x358fa88f 0x37ca1259 0x36230fe7 0x36299287 0x593e5 0x45917 0x5bfc3 0x5c487 0x5bfc3 0x5bf6b 0x45285 0x5bfc3 0x5a883 0x5e26b 0x358ca433 0x3588f533 0x3588f77b 0x358cead3 0x358ce29f 0x358cd045 0x358504a5 0x3585036d 0x374ec439 0x3335ccd5 0x42119 0x420c4)
terminate called throwing an exception
有人知道如何解决这个错误吗?
【问题讨论】:
尝试从模拟器/设备中删除应用程序并再次运行,这可能会解决您的问题。谢谢 【参考方案1】:我认为这个错误有两种可能的情况:
1) 您在之前执行应用时错误地创建了该文件。如果您使用菜单重置模拟器,这将解决:iOS 模拟器 > 重置内容和设置,并从您的设备上卸载应用程序:长按 > 单击 x 符号。
2) 在您的应用程序的其他地方有一些代码可以创建此文件。如果是这种情况,您应该找到此代码并将其删除。
或
您可以通过此链接浏览
Core Data Tips for iPhone Devs Part 2: Better Error Messages
谢谢
【讨论】:
对您的提示没有帮助...有关解决方案,请参阅我的回答【参考方案2】:“解决方案”是 .sqlite 文件必须与项目名称同名!
【讨论】:
以上是关于Core Data - 无法在 iOS 5.1 上创建持久存储的主要内容,如果未能解决你的问题,请参考以下文章
保存在 Core Data 中的 iOS 数据在启动后无法保存