Reputation: 1885
I have an exception breakpoint set and when I call - (void)saveToURL:(NSURL *)url forSaveOperation:(UIDocumentSaveOperation)saveOperation completionHandler:(void (^)(BOOL success))completionHandler on my UIManagedDocument I sometimes get the following error:
* thread #3: tid = 0x2003, 0x357a8238 libobjc.A.dylib`objc_exception_throw, stop reason = breakpoint 1.1
frame #0: 0x357a8238 libobjc.A.dylib`objc_exception_throw
frame #1: 0x3154a122 CoreData`-[NSPersistentStore(_NSInternalMethods) _preflightCrossCheck] + 758
frame #2: 0x314bb296 CoreData`-[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 970
frame #3: 0x31523286 CoreData`-[NSManagedObjectContext save:] + 522
frame #4: 0x33d0ac5a UIKit`__84-[UIManagedDocument writeContents:toURL:forSaveOperation:originalContentsURL:error:]_block_invoke_0 + 30
frame #5: 0x3152d3ee CoreData`developerSubmittedBlockToNSManagedObjectContextPerform + 74
frame #6: 0x344fa7e6 libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 26
frame #7: 0x344fa646 libdispatch.dylib`dispatch_barrier_sync_f$VARIANT$mp + 62
And in my console log in organizer I saw this:
Apr 9 12:45:47 unknown District[232] : (Error) com.apple.UIKit.UIDocument: document: fileURL: file://localhost/var/mobile/Applications/F1A7026C-5ADD-4FAF-9C8C-887514F6EACA/Documents/PLDB/ documentState: [Normal] hit writing error: Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’-t -b-e -c-o-m-p-l-e-t-e-d-. -(-C-o-c-o-a -e-r-r-o-r -1-3-4-0-3-0-.-)-" -U-s-e-r-I-n-f-o-=-0-x-5-c-1-6-8-0 -{-N-S-A-f-f-e-c-t-e-d-S-t-o-r-e-s-E-r-r-o-r-K-e-y-=-(^J -"-<-N-S-S-Q-L-C-o-r-e-: -0-x-5-b-2-7-4-0-> -(-U-R-L-: -f-i-l-e-:-/-/-l-o-c-a-l-h-o-s-t-/-v-a-r-/-m-o-b-i-l-e-/-A-p-p-l-i-c-a-t-i-o-n-s-/-F-1-A-7-0-2-6-C---5-A-D-D---4-F-A-F---9-C-8-C---8-8-7-5-1-4-F-6-E-A-C-A-/-D-o-c-u-m-e-n-t-s-/-P-L-D-B-/-S-t-o-r-e-C-o-n-t-e-n-t-/-p-e-r-s-i-s-t-e-n-t-S-t-o-r-e-)-"^J-)-, -N-S-U-n-d-e-r-l-y-i-n-g-E-r-r-o-r-=-0-x-5-e-8-0-2-0 -"-T-h-e -o-p-e-r-a-t-i-o-n -c-o-u-l-d-n’-t -b-e -c-o-m-p-l-e-t-e-d-. -(-C-o-c-o-a -e-r-r-o-r -4-.-)-"-, -N-S-F-i-l-e-P-a-t-h-=-/-v-a-r-/-m-o-b-i-l-e-/-A-p-p-l-i-c-a-t-i-o-n-s-/-F-1-A-7-0-2-6-C---5-A-D-D---4-F-A-F---9-C-8-C---8-8-7-5-1-4-F-6-E-A-C-A-/-D-o-c-u-m-e-n-t-s-/-P-L-D-B-/-S-t-o-r-e-C-o-n-t-e-n-t-/-p-e-r-s-i-s-t-e-n-t-S-t-o-r-e-} Apr 9 12:45:47 unknown District[232] : (Error) com.apple.UIKit.UIDocument: UIDocument unrecoverable error with description: The operation couldn’-t -b-e -c-o-m-p-l-e-t-e-d-. -(-C-o-c-o-a -e-r-r-o-r -1-3-4-0-3-0-.-) -r-e-a-s-o-n-: -(-n-u-l-l-) Apr 9 12:45:47 unknown District[232] : (Error) com.apple.UIKit.UIDocument: Failed to recover from error with description: The operation couldn’-t -b-e -c-o-m-p-l-e-t-e-d-. -(-C-o-c-o-a -e-r-r-o-r -1-3-4-0-3-0-.-) -r-e-a-s-o-n-: -(-n-u-l-l-)
It doesn't happen every time, so maybe a timing issue? It doesn't crash my program but breaks on my exception breakpoint and the success value is NO for my block.
Upvotes: 0
Views: 1293
Reputation: 1885
I've heard different opinions on saving core data using UIManagedDocument. Some people say don't ever call saveToUrl... except when you first create the document. However, I remember learning from the Stanford iOS course that saveToUrl... is fine to call when you explicitly want to save. Some people say to use
[document updateChangeCount:UIDocumentChangeDone]
to notify core data that it needs to save. Some say it will save itself on its own time. Some say you can use [context obtainPermanentIDsForObjects: ...] like the following category on MSManagedObjectContext to resolve temporary ID issues:
- (BOOL)obtainPermanentIDsForInsertedObjects:(NSError **)error
{
NSSet * inserts = [self insertedObjects];
if ([inserts count] == 0) return YES;
return [self obtainPermanentIDsForObjects:[inserts allObjects]
error:error];
}
I wish I knew the answer but I think my problem was that I was calling saveToUrl... too much. I removed all of my saveToUrl.. calls except the first one and things seem to be working better and faster. If you have to save the document for some reason, try one of the previous methods.
Upvotes: 1