ios - UIManagedDocument file being automatically deleted with saveToURL:forSaveOperation:completionHandler: -
i'm using core data uimanageddocument inventory-keeping app. problem i'm having "savetourl:..." method deleting uimanageddocument file in documents directory when save using uidocumentsaveforoverwriting after adding item core data. happens @ first launch new build. created core data/uimanageddocument helper singleton use throughout app.
here's how initialize uimanageddocument instance:
@interface vdfcoredatahelper : nsobject @property (strong, nonatomic) nsmanagedobjectcontext *managedobjectcontext; @property (strong, nonatomic) uimanageddocument *manageddocument; @implementation vdfcoredatahelper - (void)createmanageddocument { nsurl *docsurl = [self getdocsurl]; if (![[nsfilemanager defaultmanager] fileexistsatpath:[docsurl path]]) { nslog(@"new doc made"); _manageddocument = [[uimanageddocument alloc] initwithfileurl:docsurl]; [self savemanageddocumentforcreation]; [self openmanageddocument]; } else { nslog(@"existing doc"); _manageddocument = [[uimanageddocument alloc] initwithfileurl:docsurl]; [self openmanageddocument]; } }
createmanageddocument called in init method. have 2 save methods. 1 creating , 1 overwriting. first 1 called when created managed document.
at point, i've saved once , uimanageddocument directory , persistent store files exist in documents folder.
when want insert item object (an item entity exists), call method:
- (void)insertmanagedobject:(nsmanagedobject *)object success:(void (^)(bool successful))successblock { nsmanagedobjectcontext *context = [self context]; [context insertobject:object]; nserror *error; [context save:&error]; if (self.manageddocument.documentstate == uidocumentstatenormal) { [self.manageddocument savetourl:[self getdocsurl] forsaveoperation:uidocumentsaveforoverwriting completionhandler:^(bool success){ successblock(success); }]; } }
after "savetourl:forsaveoperation:" on writing called, managed document directory , files in documents folder automatically deleted. managedobjectcontext, item object, , manageddocument object valid @ point. document's url points correct destination, files gone.
after "insertmanagedobject" method finished, use navigation controller pop rootviewcontroller contains table view listing items. data added kept in memory , fetchedresultscontroller loads it, data not saved disk because there isn't persistent store longer. when exit app , re-enter, nothing shows , new managed document created again.
this happens if clear build , launch first time. if launched , exit, , enter app again, works fine. it's "savetourl:...: method deleting persistent store.
i've tried subclassing uimanageddocument , logging errors, doesn't show error whatsoever. i've tried commenting out of code, don't make difference.
if don't use "savetourl", persistent store doesn't deleted, upon re-launch, fetchresultscontroller.fetchobjects returns empty array , tries access non-existent indexpath, crashing app.
i'm considering ditching uimanageddocument right now. hopefully, can tell me may doing wrong, or has had same problem.
thanks.
i struggling same problem you. wasn't finding help...
the deleting part no error whatsoever driving me nuts, , ditched uimanageddocument.
but did works!.
actually think problem trying access document after creating it, calling selector:
[_document savetourl:self.documenturl forsaveoperation:uidocumentsaveforcreating completionhandler:^(bool success) {}];
then did after saving document close it, new instance , reopen it. this:
[_coredocument savetourl:self.documenturl forsaveoperation:uidocumentsaveforcreating completionhandler:^(bool success) { if (success) { nslog(@"document created"); [_coredocument closewithcompletionhandler:^(bool success) { if (success) { nslog(@"closed created document, open again"); _coredocument = nil; _coredocument = [[checkinmanageddocument alloc] initwithfileurl:self.documenturl]; [_coredocument openwithcompletionhandler:^(bool success) { nslog(@"document oppened afer creating , closing it"); [self documentisreadyforuse]; }]; } }]; } else { nslog(@"could not save document @ path: %@", self.documenturl.path); } }];
Comments
Post a Comment