objective c - Saved Core Data does not persist after app closes 80% of the time -


i started dealing core data lately, , in tests, i've found 20% of time data gets saved db. rest of time, it's saved temporarily, while app running. if restart, last data saved gets lost.

does know problem be?

here's code:

//save data nsentitydescription *users = [nsentitydescription insertnewobjectforentityforname:@"users" inmanagedobjectcontext:document.managedobjectcontext]; [users setvalue:@"name test" forkey:@"name"]; [users setvalue:[nsnumber numberwithint:20] forkey:@"age"]; [users setvalue:@"some country" forkey:@"location"];   //debugging //no error ever shows nserror *error; if(![document.managedobjectcontext save:&error]) {     nslog(@"error: %@", error); }  //this show problem may not uimanageddocument (self.document), since nslog never gets called. if(self.document.documentstate != uidocumentstatenormal) {     nslog(@"document not opened"); } //end of debugging   //fetch data entity nsfetchrequest *fetch = [nsfetchrequest fetchrequestwithentityname:@"users"]; nssortdescriptor *sortdescriptor = [nssortdescriptor sortdescriptorwithkey:@"name" ascending:yes]; fetch.sortdescriptors = [nsarray arraywithobject:sortdescriptor];  nsarray *results = [document.managedobjectcontext executefetchrequest:fetch error:nil]; nslog(@"results on database: %d", [results count]); 

document same thing (at least hope so, in of cases) self.document; it's argument of method code located.

here's code .h , .m:

.h:

#import <uikit/uikit.h> #import <coredata/coredata.h>  @interface coredataviewcontroller : uiviewcontroller  @property (nonatomic, strong) uimanageddocument *document;  @end 

.m:

#import "coredataviewcontroller.h"  @implementation coredataviewcontroller @synthesize document = _document;  - (void)fetchstuff:(uimanageddocument *)document {      //save data     nsentitydescription *users = [nsentitydescription insertnewobjectforentityforname:@"users" inmanagedobjectcontext:document.managedobjectcontext];     [users setvalue:@"name test" forkey:@"name"];     [users setvalue:[nsnumber numberwithint:20] forkey:@"age"];     [users setvalue:@"some country" forkey:@"location"];       //debugging     //no error ever shows     nserror *error;     if(![document.managedobjectcontext save:&error]) {         nslog(@"error: %@", error);     }      //this show problem may not uimanageddocument (self.document), since nslog never gets called.     if(document.documentstate != uidocumentstatenormal) {         nslog(@"document not opened");     }     //end of debugging       //fetch data entity     nsfetchrequest *fetch = [nsfetchrequest fetchrequestwithentityname:@"users"];     nssortdescriptor *sortdescriptor = [nssortdescriptor sortdescriptorwithkey:@"name" ascending:yes];     fetch.sortdescriptors = [nsarray arraywithobject:sortdescriptor];      nsarray *results = [document.managedobjectcontext executefetchrequest:fetch error:nil];     nslog(@"results on database: %d", [results count]); }  - (void)usedocument {     if(![[nsfilemanager defaultmanager] fileexistsatpath:[self.document.fileurl path]]) {         [self.document savetourl:self.document.fileurl forsaveoperation:uidocumentsaveforcreating completionhandler:^(bool success){             if(success == yes) nslog(@"created");             [self fetchstuff:self.document];         }];     } else if(self.document.documentstate == uidocumentstateclosed) {         [self.document openwithcompletionhandler:^(bool success) {             if(success == yes) nslog(@"opened");             [self fetchstuff:self.document];         }];     } else if(self.document.documentstate == uidocumentstatenormal) {         [self fetchstuff:self.document];     } }  - (void)setdocument:(uimanageddocument *)document {     if(_document != document) {         _document = document;         [self usedocument];     } }  - (void)viewwillappear:(bool)animated {     [super viewwillappear:animated];      if(!self.document) {         nsurl *url = [[[nsfilemanager defaultmanager]urlsfordirectory:nsdocumentdirectory indomains:nsuserdomainmask] lastobject];         url = [url urlbyappendingpathcomponent:@"database"];         self.document = [[uimanageddocument alloc]initwithfileurl:url];     } }  @end 

note: there's data model, has entity called "users", attributes age, location, name.

the data being saved because of autosaving, happens each x (maybe 10, need check on documentation) seconds. force save, should've used this:

[documentation savetourl:documentation.fileurl forsaveoperation:uidocumentsaveforoverwriting completionhandler:^(bool success) { if(success == yes) nslog(@"awesome, it's saved!"); }]; 

although works fine adding code fetchstuff:, it'd better implement when user exits screen, since automatically saved via autosave.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -