iphone - CoreData Save Permanently? -


i have been working core data in ipad app , can save , fetch data inside app. when closing application, fully, quit, take out of multitasking, , data disappears.

so core data in anyway keep data anywhere when app closed? or need somewhere else?

edit: in app delegate didfinishlaunchingwithoptions: [[[uiapplication sharedapplication] delegate] managedobjectcontext]; , have this: context_ = [(prototypeappdelegate *)[[uiapplication sharedapplication] delegate] managedobjectcontext]; in uiview subclass.

this nspersistentstorecoordinator code premade in app delegate:

- (nspersistentstorecoordinator *)persistentstorecoordinator {      if (persistentstorecoordinator_ != nil) {         return persistentstorecoordinator_;     }      nsurl *storeurl = [[self applicationdocumentsdirectory] urlbyappendingpathcomponent:@"prototype.sqlite"];      nserror *error = nil;     persistentstorecoordinator_ = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:[self managedobjectmodel]];     if (![persistentstorecoordinator_ addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl options:nil error:&error]) {         /*          replace implementation code handle error appropriately.           abort() causes application generate crash log , terminate. should not use function in shipping application, although may useful during development. if not possible recover error, display alert panel instructs user quit application pressing home button.           typical reasons error here include:          * persistent store not accessible;          * schema persistent store incompatible current managed object model.          check error message determine actual problem was.            if persistent store not accessible, there typically wrong file path. often, file url pointing application's resources directory instead of writeable directory.           if encounter schema incompatibility errors during development, can reduce frequency by:          * deleting existing store:          [[nsfilemanager defaultmanager] removeitematurl:storeurl error:nil]           * performing automatic lightweight migration passing following dictionary options parameter:           [nsdictionary dictionarywithobjectsandkeys:[nsnumber numberwithbool:yes],nsmigratepersistentstoresautomaticallyoption, [nsnumber numberwithbool:yes], nsinfermappingmodelautomaticallyoption, nil];           lightweight migration work limited set of schema changes; consult "core data model versioning , data migration programming guide" details.           */         nslog(@"unresolved error %@, %@", error, [error userinfo]);         abort();     }          return persistentstorecoordinator_; } 

so far using fetch data:

    nsfetchrequest *fetch = [[nsfetchrequest alloc] init];     nsentitydescription *testentity = [nsentitydescription entityforname:@"datedtext" inmanagedobjectcontext:context_];     [fetch setentity:testentity];     nspredicate *pred = [nspredicate predicatewithformat:@"datesaved == %@", datepicker.date];     [fetch setpredicate:pred];      nserror *fetcherror = nil;     nsarray *fetchedobjs = [context_ executefetchrequest:fetch error:&fetcherror];     if (fetcherror != nil) {         nslog(@"fetcherror = %@, details = %@",fetcherror,fetcherror.userinfo);     }     notetextview.text = [[fetchedobjs objectatindex:0] valueforkey:@"savedtext"]; 

and save data:

nsmanagedobject *newdatedtext;     newdatedtext = [nsentitydescription insertnewobjectforentityforname:@"datedtext" inmanagedobjectcontext:context_];     [newdatedtext setvalue:notetextview.text forkey:@"savedtext"];     [newdatedtext setvalue:datepicker.date forkey:@"datesaved"];      nserror *saveerror = nil;     [context_ save:&saveerror];     if (saveerror != nil) {         nslog(@"[%@ savecontext] error saving context: error = %@, details = %@",[self class], saveerror,saveerror.userinfo);     } 

do save context in right places? common mistake not save context when entering background application state in willterminate.

save context in following appdelegate method:

-(void)applicationdidenterbackground:(uiapplication *)application 

you saving context directly after inserting object, should sufficient. check sqlite file in simulator if contains data after saving.

if

notetextview.text = [[fetchedobjs objectatindex:0] valueforkey:@"savedtext"]; 

does not throw exception, there object found in context. maybe not contain expected value? log returned object fetchrequest console see if might case


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -