ios - Am I approaching concurrency right? -


i wondering if point me in right direction whether or not using concurrency correctly. find currently, code unwieldy , unintuitive. purposes of demonstration, please note coredata architecture described marcus zarra in article http://martiancraft.com/blog/2015/03/core-data-stack/. since operation take long time, want entire process done in background thread.

 nsmanagedcontext *context = [[nsmanagedcontext alloc] initwithconcurrency:nsprivateconcurrencyqueue];  context.parentcontext = [[coredatacontroller shareddispatch] managedobjectcontext];  [context performblock: ^{  nsmanagedobject *somemanagedobject = [[context executefetchrequest:request error:nil] firstobject];  nsstring *resultfromlongoperation = [self somelongoperation:somemanagedobject];  [self dosomething];  bool anotherresultfromlongoperation = [self adifferentlongoperation:somemanagedobject];  }]; 

so short example, can see manipulating variable somemanagedobject, of nsmanagedobject type , therefore must used in performblock/performblockandwait prescribed apple. should passing in context somelongoperation , adifferentlongoperation? if don't, wouldn't mean have create child context , whatever function inside performblock/performblockandwait , return result __block type? in event if result of somelongoperation affects response of adifferentlongoperation? still structuring code situation this?

thanks in advanced!

i struggled same issue not long ago.

i've looked best practice scenarios , found that:

  • you indeed, stated, shouldn't violate 1 context/thread rule set apple.
  • you should use completion blocks or notifications long running operations, whether it's not making ui freeze, or code clarity.
  • when use completion block, can create child context in long running operation function (somelongoperation) , whatever want it.
  • if result nsstring, shouldn't have problem, because it's not nsmanagedobject , not bound 1 context.
  • if result nsmanagedobject, should pass nsmanagedobjectid completion block (with [managedobject objectid] method).
  • you can use context's method obtainpermanentidsforobjects in case created new entity , doesn't have object id yet.

basically, can use same context every long running operation if use same thread. in case don't i'm creating new child context every long running operation sure (creating new child context fast operation performance isn't issue)


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -