-processPendingChanges removing changed objects from -updatedObjects list
-processPendingChanges removing changed objects from -updatedObjects list
- Subject: -processPendingChanges removing changed objects from -updatedObjects list
- From: Daniel DeCovnick <email@hidden>
- Date: Thu, 08 Jul 2010 14:50:59 -0700
Hi all,
I'm really stumped by something. The short version is that calling -
[NSManagedObjectContext processPendingChanges] is actually removing
changed managed objects from the list returned by -
[NSManagedObjectContext updatedObjects]. Anyone know why this would
happen?
The long version is as follows:
Background: I have 3 entities, A, B, BRef. BRef acts as a join table
with an index for A and B, because the relationship between As and Bs
needs to remain ordered:
A(name, ...)<--owner--childReferences-->>BRef(index)<<--items--
references-->B(name, url,...)
In A, I have implemented (or rather, been given an implementation of)
a custom setter method setBs: which creates the BRef instances for
each B in the passed array:
- (void)setBs:(NSArray *)children
{
[mCachedChildren release];
mCachedChilren = nil;
mCachedChildren = [children copy];
NSManagedObjectContext *context = [[Library sharedLibrary]
contextForCurrentThread];
NSMutableSet *references = [NSMutableSet set];
for (NSUInteger index = 0; index < children.count; index++) {
B *item = [children objectAtIndex:index];
BRef *reference = [Bref insertInManagedObjectContext:context];
//^^ This is from MOGenerator
reference.item = item;
reference.index = [NSNumber numberWithInteger:index];
[references addObject:reference];
}
NSSet *oldReferences = self.childReferences;
for (BRef *reference in oldReferences) {
[context deleteObject:reference];
}
self.childReferences = references;
}
What I'm doing several stack frames up is to generate a whole bunch of
As from an external data source, the Bs for which already exist. In
the loop to generate them, I take each B, and replace it with a
doppelgänger which comes from a less transient source. I do a fetch
request to get the original B's replacement, populate a replacements
array, and then call the code which calls setBs: above.
So far, everything was working fine.
Then I decided I didn't like the performance of this, and on
investigation, discovered that I was calling [context save:&error]
way, way too much. So I decided I'd go ahead and do all the changes to
the object graph, then write it out in one big save. Sounded like a
great idea, so I did it.
Turned out that there was one little problem: all but the first of my
new As were empty! Well, not empty, but a tableview owned by another
class, AController, was; it conditionally reloads data on the
NSManagedObjectContextObjectsDidChangeNotification; each AController
checks to see if its A is in [[notification userInfo]
objectForKey:NSUpdatedObjectsKey], and refreshes itself if so. (Yes,
there is an AControllerManager, and I realize that it should be the
one registered for that notification, but that's not related to the
problem.)
Clearly the A's are changing: their BRefs are being set. Tracing the
problem and logging the output of [context updatedObjects], I found
that on every first fetch request, the A would be removed from -
updatedObjects list. A little documentation reading and testing
revealed that the -processPendingChanges in the fetch request would
remove the changed B from the list!
So I'm well and truly stumped. Functionally, the only difference
between the code paths were doing before and what they're doing now is
only the frequency of save: operations (incorrectly frequently to none
until the final save). What's really odd is that if I quit and
restart, the As are properly populated with their B doppelgängers, so
the whole object graph IS getting saved properly.
Any help or insight would be greatly appreciated!
Thanks,
Daniel
Daniel DeCovnick
danhd123 at mac dot com_______________________________________________
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