objective c - Undoing Core Data insertions that are performed off the main thread -
i'm working on code uses nsoperation
import data. i'd user able undo nsmanagedobject
instances created during import operation.
from can tell, it's impossible use nsmanagedobjectcontext
-undomanager
operations performed off of main thread. core data programming guide section on use thread confinement support concurrency, have these 2 conditions:
- only objectid should passed between managed object contexts (on separate threads)
- managed objects must saved in context before objectid can used.
this makes sense since managed objects need moved private storage (nsmanagedobjectcontext
) public storage (nspersistentstore
) before can shared.
unfortunately, -save:
message causes managed objects in undo stack removed. memory management using core data section of same guide:
managed objects have pending changes (insertions, deletions, or updates) retained context until context sent save:, reset , rollback, or dealloc message, or appropriate number of undos undo change.
i've tried several things work around limitation, , leads bulk of work happening on main thread (and spinning beach balls.) clues getting undo working objects created off main thread appreciated.
--
an enhancement radar has been submitted: rdar://problem/8977725
this answer bit of , forth. if understand issue correctly, doing import when import done want user able select gets saved import?
if not correct, please fix assumptions , update answer.
if correct can is:
change background object creation
nsentitydescription *myentity = ... //entity context [[nsmanagedobject alloc] initwithentity:myentity insertintomanagedobjectcontext:nil];
- store these entities in array.
- pass entities main thread needed.
- release on objects don't want keep
- call
[mymaincontext insertobject:managedobject]
on want keep. - perform save on
nsmanagedobjectcontext
.
since these entities not part of nsmanagedobjectcontext
yet exist in memory , should thread safe since not yet tied down nsmanagedobjectcontext
.
this of course theoretical , require testing. should accomplish goal.
Comments
Post a Comment