Core Data: NSCocoaErrorDomain=134040 Only Occurs On Real Phone, Not Simulator
I'm using Core Data to save a persistent store in the document directory of my application. When I attempt to save my managed context changes, I get a Cocoa Error 134040. This only occurs on the real iPhone in debug mode, not on the simulator. Any idea why this occurs?
This is how I initially create my data store in the documents directory.
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]);
}
This is how I save the context:
[managedObjectContext_ save:&error];开发者_开发知识库
This is the error I get when trying to save the managed object context:
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]}
MORE CLARIFICATION
I actually have 2 persistent stores. One is suppose to be read only data that doesn't change in between upgrades and it resides in the main bundle. The other is a data store for things saved by the user, and this resides inside the Document folder. Both persistent stores belong to the same managed object context, but I use configurations to only save certain entities to each (i.e. read only entities go in one, user saved entities goes in another).
NSPersistentStore *readonlyStore = [persistentStoreCoordinator_
addPersistentStoreWithType:NSBinaryStoreType
configuration:@"ReadOnlyData"
URL:readonlyStoreURL
options:nil
error:&error];
When you get an error, look it up. This one is in CoreDataErrors.h (there are also FoundationErrors.h and a few others):
NSPersistentStoreIncompleteSaveError = 134040, // one or more of the stores returned an error during save (stores/objects that failed will be in userInfo)
So, you couldn't save, and the userInfo tells you why.
Indeed it does. From your question:
NSUnderlyingException=Save partially failed: Directory does not exist or is not writable /var/mobile/Applications/...[app bundle path]}
You're trying to save into your app bundle. That won't work; your app bundle is not writable (certainly not on the device).
You showed the creation of a persistent store in the documents directory. As far as I can tell by reading it, that should be working. Do you have any other persistent stores, perhaps that you intended to default to the contents of/migrate from? If so, please edit your question to include that code.
精彩评论