ios - Multiple NSManagedObjectContexts or single context and -performBlock -


i have been using core data single nsmanagedobjectcontext long time, fetching, saving, background update operations done on single context through helper classes, planning implement multiple nsmanagedobjectcontext approach (which recommended solution in of searching).

my question is: performblock execute code context? can't below:

- (void) checksyncserver {     dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_background, 0), ^{         //do here, check , fetch data         // create nsmanagedobject's          [_tempcontext save:&error];           //mastercontext merge changes through notification observers     }); } 

(i.e) execute code apart -performblock method. how can execute multiple asynchronous methods , perform save?

however, find single context (which managed 1 singleton nsobject class) simpler use.

this multiple context contextconcurrencytype looks more complicated (in terms of execution flow). there better solution?

you can access contexts in 1 of 2 ways:

  • on thread/queue. applies confined contexts , main queue contexts. can access them freely own thread.
  • with -performblock: if private queue context or if touching context thread other 1 belongs on.

you cannot use dispatch_async access context. if want action asynchronous need use -performblock:.

if using single context before , touching dispatch_async violating thread confinement rule.

update

when call [[nsmanagedobjectcontext alloc] init] functionally equivalent [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsconfinementconcurrencytype].

the nsmanagedobjectcontext has always been thread confined.

as executing multiple methods can call them in same block:

nsmanagedobjectcontext *moc = ...; [moc performblock:^{   //fetch   //process data   //save }]; 

or nest them if wanted them async of each other:

nsmanagedobjectcontext *moc = ...; [moc performblock:^{   //fetch   [moc performblock:^{     //process data   }];   [moc performblock:^{     //save   }]; }]; 

since -performblock: re-entrant safe can nest them want.

update async save

to async save should have 2 contexts (or more):

  • main queue context ui talks to
  • private queue context saves

private context has nspersistentstorecoordinator , main queue context has private parent.

all work done in main queue context , can save safely, on main thread. save instantaneous. afterwards, async save:

nsmanagedobjectcontext *privatemoc = ...; nsmanagedobjectcontext *mainmoc = ...;  //do on mainmoc  nserror *error = nil; if (![mainmoc save:&error]) {   nslog(@"main moc save failed: %@\n%@", [error localizeddescription], [error userinfo]);   abort(); }  [privatemoc performblock:^{   nserror *error = nil;   if (![privatemoc save:&error]) {     nslog(@"private moc failed write disk: %@\n%@", [error localizeddescription], [error userinfo]);     abort();   } }]; 

if have app, need is:

  • create private moc
  • set parent of main
  • change main's init
  • add private block save method whenever call save on main

you can refactor there really need change.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -