ios - Merging main and private contexts with Core Data -
i'm creating temportary contexts in private queue asynchronously update data persist core data
:
nsmanagedobjectcontext *privatecontext = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsprivatequeueconcurrencytype]; appdelegate *appdelegate = [[uiapplication sharedapplication] delegate]; privatecontext.persistentstorecoordinator = appdelegate.persistentstorecoordinator; [privatecontext performblock: ^{ // parse files and/or call services , parse // responses dispatch_async(dispatch_get_main_queue(), ^{ // notify update user }); }];
then, once i've got new data, need merge changes main context. know common scenario, i'm not sure how should proceed... apple's core data documentation section talks setting merge policy , don't understand way handle that. on other hand, found link, scenario described in "stack #2" section , says looks simpler, , doesn't talk merge policies @ all...
which correct or appropriate way should be? i'd appreciate explanation and/or simple example of how manage scenario.
thanks in advance.
what have there looks pretty good.
you using private queue work, , it's being saved persistent store.
if have small number of changes, fine. in case, want handle nsmanagedobjectcontextdidsavenotification
context, , merge changes other context with
[context mergechangesfromcontextdidsavenotification:notification];
however, if doing lot of changes, want separate persistent store coordinator (attached same store). doing this, can write store, while mocs on other psc reading. if share psc other readers, 1 access @ time , cause readers block until write has finished.
also, if doing lots of changes, make sure them in small batches, inside autoreleasepool, saving after each batch. take @ similar question: where should nsmanagedobjectcontext created?
finally, if doing lots of changes, may more efficient refetch data process merges. in case, don't need observe notification, , don't need merge. it's pretty easy. note, if this, should have separate psc... if want save , notify in small-ish batches.
[privatecontext performblock: ^{ // parse files and/or call services , parse // responses dispatch_async(dispatch_get_main_queue(), ^{ // refetch data want... if on ios, simple // telling fetched results controller refetch, , // reloading table view (or whatever else using data). }); }];
Comments
Post a Comment