ios - Core data background processing still blocks UI -


for app, i'm fetching quite lot objects core data store, causes app freeze , blocks ui input. want fetching in background while app remains responsive , update tableview when data available.
purpose i've setup new nsmanagedobjectcontext nsprivatequeueconcurrencytype , made child of main moc. while setup returning desired objects seems processing still freezing ui , there no difference in responsiveness old code happening on main queue.

according article child contexts setup not on keeping ui responsive while everywhere else on net read way go if want relieve main queue heavy processing? missing ?

  nsmanagedobjectcontext *mainmoc = self.mainobjectcontext;   nsmanagedobjectcontext *backgroundmoc = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsprivatequeueconcurrencytype];      [backgroundmoc setparentcontext:mainmoc];     [backgroundmoc performblock:^{          //query objects         nsarray *results = [product mr_findallincontext:backgroundmoc];              nserror *childerror = nil;              [backgroundmoc save:&childerror];           if ( [results count] > 0 ) {             //get objectids             nsmutablearray *objectids = [nsmutablearray array]             (nsmanagedobject *object in results) {             [objectids addobject:[object objectid]];             }              [mainmoc performblock:^{                 //refetch objects on mainqueue                 nsmutablearray *persons = [nsmutablearray array]                 (nsmanagedobjectid *objectid in objectids) {                 [persons addobject:(person*)[mainmoc objectwithid:objectid]];                 }                  //return result                  if (self.callback)                  self.callback(persons);             }];         }     }]; 

first of all, helpful know mr_findallincontext: exactly. best solution solve in more efficient way. how predicate like? specify batch size on request? yo use indexes on attributes you're querying for? what's size of data set? it's hard tell if there better solution without more details.

you're current approach suffers seems pretty widespread misunderstanding of how nested contexts work.

the problem way contexts setup. since make background context child of main context, in background context has go "through" main context.

saving background context result in pushing changes in object graph main context has save persist changes. doing fetch request on background context forward main context, send persistent store coordinator , hands results background context synchronously. request on background context (fetch or save) lock parent context , block main thread when request directly on main context.

adding background context behind main thread context not going fly performance wise. nested contexts not made used in way.

in order achieve want, have perform fetch request on context independent main context, e.g. background context directly associated psc. in case fetch request still lock psc. means executing request on main context during time still block main thread, because of lock contention on psc. @ least main thread not blocked in general.

be aware though when hand resulting objectids main context, objects there objectwithid: , access these objects, you're relying on psc's row cache still hold data in order fast. since objects faults @ first, core data have go disk every object if row cache doesn't have data anymore. slow. can check instruments cache hits , misses.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -