objective c - Core Data: How to merge inserts/updates/deletes between two NSManagedObjectContext's while maintaining the merge as an undoable step? -


i have document-based core data application (running on mac os x 10.5 , above) i'm trying use 2 nsmanagedobjectcontext's on main thread. i'd merge changes made in secondary context main (primary) context. in addition, want changes merged in secondary context undoable , cause document marked "dirty". guess question similar "undoing core data insertions performed off main thread" but, atm, i'm not using different threads.

i've been observing nsmanagedobjectcontextdidsavenotification (which gets sent second context when calling -[self.secondarycontext save:]) this:

[[nsnotificationcenter defaultcenter] addobserver:self                                          selector:@selector(mocdidsave:)                                              name:nsmanagedobjectcontextdidsavenotification                                            object:self.secondarycontext]; 

in -mocdidsave: method called observer tried use -[nsmanagedobjectcontext mergechangesfromcontextdidsavenotification:] on primary context merge changes secondary context primary context:

- (void)mocdidsave:(nsnotification *)notification {     [self.primarycontext mergechangesfromcontextdidsavenotification:notification]; } 

however, while the, say, inserted objects readily appear in array controller, document not marked dirty , isinserted property of newly added managed objects not set yes. insertion (into primary context) not undoable.

re-faulting inserted objects @ least mark document dirty insertion still not undoable:

- (void)mocdidsave:(nsnotification *)notification {     [self.primarycontext mergechangesfromcontextdidsavenotification:notification];      (nsmanagedobject *insertedobject in [[notification userinfo] objectforkey:nsinsertedobjectskey]) {         [self.primarycontext refreshobject:[self.primarycontext existingobjectwithid:[insertedobject objectid] error:null] mergechanges:no];     } } 

w.r.t. -mocdidsave:, had better results custom implementation:

- (void)mocdidsave:(nsnotification *)notification {     nsdictionary *userinfo = [notification userinfo];      nsset *insertedobjects = [userinfo objectforkey:nsinsertedobjectskey];     if ([insertedobjects count]) {         nsmutablearray *newobjects = [nsmutablearray array];         nsmanagedobject *newobject = nil;         (nsmanagedobject *insertedobject in insertedobjects) {             newobject = [self.primarycontext existingobjectwithid:[insertedobject objectid] error:null];             if (newobject) {                 [self.primarycontext insertobject:newobject];                 [newobjects addobject:newobject];             }         }          [self.primarycontext processpendingchanges];          (nsmanagedobject *newobject in newobjects) {             [self.primarycontext refreshobject:newobject mergechanges:no];         }     }      nsset *updatedobjects = [userinfo objectforkey:nsupdatedobjectskey];     if ([updatedobjects count]) {         nsmanagedobject *staleobject = nil;         (nsmanagedobject *updatedobject in updatedobjects) {             staleobject = [self.primarycontext objectregisteredforid:[updatedobject objectid]];             if (staleobject) {                 [self.primarycontext refreshobject:staleobject mergechanges:no];             }         }     }      nsset *deletedobjects = [userinfo objectforkey:nsdeletedobjectskey];     if ([deletedobjects count]) {         nsmanagedobject *staleobject = nil;         (nsmanagedobject *deletedobject in deletedobjects) {             staleobject = [self.primarycontext objectregisteredforid:[deletedobject objectid]];             if (staleobject) {                 [self.primarycontext deleteobject:staleobject];             }         }          [self.primarycontext processpendingchanges];     } } 

this causes array controller updated, document gets marked dirty, , insertions & deletions undoable. however, i'm still having problems updates aren't yet undoable. should instead manually loop on updatedobjects , use -[nsmanagedobject changedvalues] reapply changes in primary context?

this custom implementation would, of course, duplicate lot of work secondary context on main context. there other/better way of getting merge between 2 contexts while maintaining merge undoable step?

if not using separate threads, don't need separate contexts. using 2 context on same thread adds complexity without gaining anything. if don't know employ threads, highly recommend using 1 context. premature optimization root of evil.

saves reset undo manager can't use nsmanagedobjectcontextdidsavenotification perform operation can undone. found can trick app thinking document dirty can't force undo manager remember past last save.

the way that, unlimited undo, save multiple versions of doc behind scenes. iirc, can serialize undo manager can written file , reloaded backtrack.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -