ios - Handling heavy deletions/insertions in Core Data -


i'm dealing scenario there objects which, deleted, cause exponential amount of deletions through cascade-delete relationships. in way, deleting 20 of these objects can result in ~3,000 objects being deleted. can cause slow saves, if performed main context.

to combat this, i'm creating 'worker' context uses same persistent store main context, making changes there, saving worker context , merging changes main context:

nsmanagedobjectcontext *workercontext = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsprivatequeueconcurrencytype];  workercontext.persistentstorecoordinator = maincontext.persistentstorecoordinator;  [notificationcenter addobserver:self selector:@selector(workercontextdidsave:) name:nsmanagedobjectcontextdidsavenotification object:workercontext];  //  here 'superficial' deletion on main context, ui updates, //  actual deletion on worker context. save worker context:  [workercontext save:nil];  // fires spawnedworkercontextdidsave:, merge changes main context:  [maincontext performblockandwait:^{     [maincontext mergechangesfromcontextdidsavenotification:notification]; }]; 

my question(s) are: what's common way of handling these kinds of large deletion batches? there pitfalls approach above? additionally, merge causes small lag on main thread, nothing near when did deletions , save on main thread.

the approach of deleting data via second context fine.

you may find works faster if first prefetch data: execute fetch request objects delete in background context, , set relationships prefetch. pull data row cache (memory) before delete, can save trips disk.

if find delay merging changes on main thread problem, can instead consider resetting main context. downside there app has refetch data. could, example, fire notifications warn different parts of app reset a) going happen, , b) has happened.

lastly, there api in core data performing large deletes in background without pulling data memory. main downside there have careful merge deleted objects yourself, because context not know have been deleted.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -