核心数据:NSCocoaErrorDomain = 134040只发生在真正的手机,而不是模拟器

我正在使用核心数据来保存我的应用程序的文档目录中的持久存储。 当我试图保存我的托pipe上下文更改时,我得到一个Cocoa错误134040.这只发生在debugging模式下的真实iPhone,而不是在模拟器上。 任何想法为什么发生这种情况

这是我最初在文档目录中创build数据存储的方式。

NSString *documentDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSURL *userDataStoreURL = [NSURL fileURLWithPath:[documentDirPath stringByAppendingPathComponent:@"userdata.coredata"]]; userDataStore= [persistentStoreCoordinator_ addPersistentStoreWithType:NSBinaryStoreType configuration:@"UserData" URL:userDataStoreURL options:nil error:&error]; NSFileManager *fileManager = [NSFileManager defaultManager]; # The following IF statement never executes, so I know the file is being created. if(![fileManager fileExistsAtPath:[userDataStoreURL path]]) { NSLog(@"User data file does not exist: %@", [userDataStoreURL path]); } 

这是我如何保存上下文:

 [managedObjectContext_ save:&error]; 

这是我尝试保存托pipe对象上下文时得到的错误:

 Error Domain=NSCocoaErrorDomain Code=134040 "The operation couldn't be completed. (Cocoa error 134040.)" UserInfo=0x607b80 {NSAffectedStoresErrorKey=(  "<NSBinaryObjectStore: 0x23f870>",    (    "<NSBinaryObjectStore: 0x23f870>"  ) ), NSUnderlyingException=Save partially failed: Directory does not exist or is not writable /var/mobile/Applications/...[app bundle path]} 

更多的澄清

我其实有2个持久性商店。 一个是假设只读取升级之间不会改变的数据,它位于主包中。 另一个是由用户保存的东西的数据存储,并且它驻留在Document文件夹中。 这两个持久性存储属于相同的托pipe对象上下文,但我使用configuration只保存某些实体(即只读实体进入一个,用户保存实体进入另一个)。

 NSPersistentStore *readonlyStore = [persistentStoreCoordinator_ addPersistentStoreWithType:NSBinaryStoreType configuration:@"ReadOnlyData" URL:readonlyStoreURL options:nil error:&error]; 

当你得到一个错误,查找它。 这个是在CoreDataErrors.h(也有FoundationErrors.h和其他几个):

  NSPersistentStoreIncompleteSaveError = 134040, // one or more of the stores returned an error during save (stores/objects that failed will be in userInfo) 

所以,你不能保存,而userInfo告诉你为什么。

确实如此。 从你的问题:

 NSUnderlyingException=Save partially failed: Directory does not exist or is not writable /var/mobile/Applications/...[app bundle path]} 

您正试图保存到您的应用程序包。 这是行不通的。 你的应用程序包不可写(当然不在设备上)。

您在文档目录中显示了持久性存储的创build。 据我所知,阅读它应该是有效的。 你有没有其他的持久性商店,也许你打算默认的内容/从/迁移? 如果是这样,请编辑您的问题,包括该代码。