ios - Core Data not persisting: Using magicalrecord -
alright,
i'm using magicalrecord core data implementation. app downloads bunch of info first time loaded. data saved core data. done in appdelegate file. but, whenever app goes background or terminated data lost. here didfinishlaunching options method:
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { nslog(@"my app starting"); [magicalrecord setupautomigratingcoredatastack]; //nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; if (![[nsuserdefaults standarduserdefaults] objectforkey:@"agm_datasetup"]) { nserror* jsonerror; nsarray* json; reachability *reachability = [reachability reachabilityforinternetconnection]; networkstatus internetstatus = [reachability currentreachabilitystatus]; if (internetstatus == notreachable) { nslog(@"offline"); nsstring *filepath = [[nsbundle mainbundle] pathforresource:@"static" oftype:@"json"]; nsstring *myjson = [[nsstring alloc] initwithcontentsoffile:filepath encoding:nsutf8stringencoding error:null]; json = [nsjsonserialization jsonobjectwithdata:[myjson datausingencoding:nsutf8stringencoding] options:kniloptions error:&jsonerror]; } else { nslog(@"online"); nsdata* data = [nsdata datawithcontentsofurl: awaterurl]; json = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&jsonerror]; } if (!jsonerror) { [magicalrecord cleanup]; nsstring* folderpath = [nssearchpathfordirectoriesindomains(nslibrarydirectory, nsuserdomainmask, yes) objectatindex:0]; nserror *error = nil; (nsstring *file in [[nsfilemanager defaultmanager] contentsofdirectoryatpath:folderpath error:&error]) { [[nsfilemanager defaultmanager] removeitematpath:[folderpath stringbyappendingpathcomponent:file] error:&error]; if(error) { nslog(@"delete error: %@", error.description); } } [magicalrecord setupcoredatastackwithautomigratingsqlitestorenamed:@"anglers411.sqlite"]; nslog(@"initial data load"); (id jsonarea in json) { // create area area *area = [area mr_createentity]; area.name = [jsonarea objectforkey:@"name"]; area.bulletdescription = [jsonarea objectforkey:@"bulletdescription"]; area.uid = [jsonarea objectforkey:@"id"]; nsarray* jsonlocations = [jsonarea objectforkey:@"locations"]; (id jsonlocation in jsonlocations) { // create location location *location = [location mr_createentity]; location.uid = [jsonlocation objectforkey:@"id"]; location.name = [jsonlocation objectforkey:@"name"]; location.desc = [jsonlocation objectforkey:@"desc"]; location.directions = [jsonlocation objectforkey:@"directions"]; location.bulletdescription = [jsonlocation objectforkey:@"bulletdescription"]; location.area = area; nsarray* jsonbugs = [jsonlocation objectforkey:@"bugs"]; (id jsonbug in jsonbugs) { bug *bug = [bug mr_createentity]; bug.name = [jsonbug objectforkey:@"name"]; bug.spring = [jsonbug objectforkey:@"spring"]; bug.summer = [jsonbug objectforkey:@"summer"]; bug.fall = [jsonbug objectforkey:@"fall"]; bug.winter = [jsonbug objectforkey:@"winter"]; bug.location = location; } } } [[nsmanagedobjectcontext mr_defaultcontext] mr_savetopersistentstorewithcompletion:^(bool success, nserror *error) { if (success) { nslog(@"you saved context."); } else if (error) { nslog(@"error saving context: %@", error.description); } }]; // set user default prevent preload of data on startup. [[nsuserdefaults standarduserdefaults] setbool:yes forkey:@"agm_datasetup"]; [[nsuserdefaults standarduserdefaults] synchronize]; } } return yes; }
here method use in both applicationdidenterbackground , applicationwillterminate:
- (void)savecontext { [magicalrecord cleanup]; }
thanks this. have read on every magicalrecord non-persist question on here , haven't found worked.
take care,
ben
sorry,
after further exploration, see not going work set 2 core data stacks different names, if want them same stack... have corrected this.
Comments
Post a Comment