KVO on a managed object called twice in child context
KVO on a managed object called twice in child context
- Subject: KVO on a managed object called twice in child context
- From: Roland King <email@hidden>
- Date: Thu, 18 Apr 2013 00:25:43 +0800
I have an NSManagedObject "masterObject" in my main context with an NSOrderedSet property on it "characterLists". I'm observing that property in a UITableView and making the necessary changes when things are inserted, deleted etc. This has worked fine up until now. I just wrote a data importer which takes the contents of a file and adds it to the database, potentially adding one or more objects to the characterLists property.
For ease of isolation I make a new MOC, parent it to the MOC masterObject is in, and then load masterObject into it using its object ID like this
NSManagedObjectContext *moc = [ [ NSManagedObjectContext alloc ] initWithConcurrencyType:NSMainQueueConcurrencyType ];
moc.parentContext = database.managedObjectContext;
RKCHDBMasterObject *masterObject = database.masterObject;
RKCHDBMasterObject *mocMasterObject = (RKCHDBMasterObject*)[ moc objectWithID:masterObject.objectID ];
Then I do all my adds in the child context, moc, and add any new characterLists to the mocMasterObject. If the file parses correctly, I save the child context, else I just toss it away and put up an appropriate error message, the line of code for that is
NSError *saveError = nil;
if( [ moc save:&saveError ] ) // <---- HERE
[ database saveContext ];
In the case where my file parse causes me to add to 'characterLists', adding just one object, I'm getting *two* KVO insert notifications on the original master object telling me the same object was inserted both times. They are exactly the same call and both of them are in the line highlighted, when saving the child context, it's not even one on the child and one on the master save. It's the same KVO notification, same index set, same type and the 'inserted' object is exactly the same as well, same address, everything.
This of course breaks the world because I add a row in my tableview for the same thing twice but the actual characterLists property only gets bigger by one, so the tableview barfs.
I'm not registering for KVO twice, I checked that. There are no other KVO messages removing or resetting the thing between the two inserts.
If I use the original parent MOC and original masterObject, not a problem, one KVO call.
Is there anything obvious I could be doing wrong here or something else I could look for, or am I returning to bugreporter again?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden