Invalidated managed objects
Invalidated managed objects
- Subject: Invalidated managed objects
- From: Gideon King <email@hidden>
- Date: Sat, 3 Apr 2010 14:18:37 +1000
When I create a document, save it, then save as, then save as again, it duplicates the persistent store, so the managed objects I have been using in my application are all invalidated.
Now there are a whole lot of places in my application where I have KVO set up on properties of the managed objects, and some places where I have the managed objects set as instance variables of my objects. I was not expecting them to be invalidated by the save as operation.
I have created a mini project that reproduces this problem with a simple atomic store and a data model with one entity.
I presume my KVOs should be OK, since they are looked up at runtime based on key paths, but the instance variables throughout my application would presumably all have to be re-fetched.
Anyway, I found that there is a notification that tells you when the objects are invalidated, so I put the following in my init method of my NSPersistentDocument subclass:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(managedObjectsChanged:) name:NSManagedObjectContextObjectsDidChangeNotification object:[self managedObjectContext]];
And created a method to refresh my data:
- (void)managedObjectsChanged:(NSNotification *)notification {
if ([[notification userInfo] objectForKey:NSInvalidatedObjectsKey]) {
NSSet *objects = [[notification userInfo] objectForKey:NSInvalidatedObjectsKey];
for (NSManagedObject *obj in objects) {
[[self managedObjectContext] refreshObject:obj mergeChanges:YES];
}
} else if ([[notification userInfo] objectForKey:NSInvalidatedAllObjectsKey]) {
for (NSManagedObject *obj in [[self managedObjectContext] registeredObjects]) {
[[self managedObjectContext] refreshObject:obj mergeChanges:YES];
}
}
}
I have confirmed that this is called in the save/save as/save as scenario, with the NSInvalidatedAllObjectsKey, and my object is getting sent the refreshObject message...But when I go to access my data, it still tells me that it has been invalidated. Am I doing something wrong here? The fact that the error is that the object is invalid rather than trying to message a released object makes me thing that the actual pointer to the object doesn't change, but that the data should be reloaded...but it's not. Any clues or suggestions?
Thanks
Gideon
_______________________________________________
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