ios - Core data saving on child context not working -
for concurrency in coredata using parent-child moc concept parent context of nsmainqueueconcurrencytype
, child context of nsprivatequeueconcurrencytype
, below code
- (nsmanagedobjectcontext *)managedobjectcontext { if (managedobjectcontext != nil) { return managedobjectcontext; } nspersistentstorecoordinator *coordinator = [self persistentstorecoordinator]; if (coordinator != nil) { managedobjectcontext = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsmainqueueconcurrencytype];// done avoid crash when app auto logs out managedobjectcontext.persistentstorecoordinator = coordinator; } return managedobjectcontext; } -(nsmanagedobjectcontext *)getprivatemoc { nsmanagedobjectcontext *private = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsprivatequeueconcurrencytype]; [private setparentcontext:[self managedobjectcontext]]; return private; } -(void)storecustomerdata:(nsarray *)custdata { nsmanagedobjectcontext *currentbgcontext = [self getprivatemoc]; [currentbgcontext performblock:^{ (nsdictionary *jsonobject in custdata) { customerentity *custentity = [nsentitydescription insertnewobjectforentityforname:@"customerentity" inmanagedobjectcontext:currentbgcontext]; custentity.fname = [jsonobject field:@"fname"]; custentity.lname = [jsonobject field:@"lname"]; } nserror *error = nil; if (![currentbgcontext save:&error]) { nslog(@"error saving context: %@\n%@", [error localizeddescription], [error userinfo]); //abort(); } }];
}
if use parent context in method storecustomerdata
works using child context getprivatemoc()
doesn't.
please let me know if missing anything
i guess understood problem, please try below code
// used propegate saves persistent store (disk) without blocking ui - (nsmanagedobjectcontext *)mastermanagedobjectcontext { if (_mastermanagedobjectcontext != nil) { return _mastermanagedobjectcontext; } nspersistentstorecoordinator *coordinator = [self persistentstorecoordinator]; if (coordinator != nil) { _mastermanagedobjectcontext = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsprivatequeueconcurrencytype]; _mastermanagedobjectcontext.retainsregisteredobjects = yes; //to-do : need update later [_mastermanagedobjectcontext performblockandwait:^{ [_mastermanagedobjectcontext setpersistentstorecoordinator:coordinator]; // [_mastermanagedobjectcontext setundomanager:nil]; }]; } return _mastermanagedobjectcontext; } // return nsmanagedobjectcontext used in background during sync - (nsmanagedobjectcontext *)backgroundmanagedobjectcontext { if (_backgroundmanagedobjectcontext != nil) { return _backgroundmanagedobjectcontext; } nsmanagedobjectcontext *mastercontext = [self mastermanagedobjectcontext]; if (mastercontext != nil) { _backgroundmanagedobjectcontext = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsprivatequeueconcurrencytype]; [_backgroundmanagedobjectcontext performblockandwait:^{ [_backgroundmanagedobjectcontext setparentcontext:mastercontext]; }]; } return _backgroundmanagedobjectcontext; } // return nsmanagedobjectcontext used in background during sync - (nsmanagedobjectcontext *)newmanagedobjectcontext { nsmanagedobjectcontext *newcontext = nil; nsmanagedobjectcontext *mastercontext = [self mastermanagedobjectcontext]; if (mastercontext != nil) { newcontext = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsmainqueueconcurrencytype]; [newcontext performblockandwait:^{ [newcontext setparentcontext:mastercontext]; }]; } return newcontext; } - (void)savemastercontext { [self.mastermanagedobjectcontext performblockandwait:^{ nserror *error = nil; bool saved = [self.mastermanagedobjectcontext save:&error]; if (!saved) { // real error handling //nslog(@"could not save master context due %@", error); } }]; } - (void)savebackgroundcontext { [self.backgroundmanagedobjectcontext performblockandwait:^{ nserror *error = nil; bool saved = [self.backgroundmanagedobjectcontext save:&error]; if (!saved) { // real error handling //nslog(@"could not save background context due %@", error); } }]; } static dispatch_queue_t coredata_background_save_queue; dispatch_queue_t background_save_queue() { if (coredata_background_save_queue == null) { coredata_background_save_queue = dispatch_queue_create("com.magicalpanda.coredata.backgroundsaves", 0); } return coredata_background_save_queue; } - (void)savedatainbackgroundwithcontext:(void(^)(nsmanagedobjectcontext *context))saveblock { dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_background, 0), ^{ [self savedataincontext:saveblock]; }); } - (void)savedataincontext:(void(^)(nsmanagedobjectcontext *context))saveblock { nsmanagedobjectcontext *context = [[efcoredatacontroller sharedinstance] backgroundmanagedobjectcontext]; [context setmergepolicy:nsmergebypropertyobjecttrumpmergepolicy]; [[self objectcontext] setmergepolicy:nsmergebypropertystoretrumpmergepolicy]; // [[self objectcontext] observecontext:context]; //step 3 // block(context); //step 4 if ([context haschanges]) //step 5 { nserror *e; [[self objectcontext] save:&e]; //magicalrecord dump errors console save method } }
Comments
Post a Comment