Re: Persistent store removal throwing invalidated NSManagedObject exceptions
Re: Persistent store removal throwing invalidated NSManagedObject exceptions
- Subject: Re: Persistent store removal throwing invalidated NSManagedObject exceptions
- From: Jonathan Dann <email@hidden>
- Date: Wed, 1 Apr 2009 12:58:53 +0200
On 1 Apr 2009, at 11:20, Drew McCormack wrote:
I've setup a temporary managed object context to export data to an
external XML file (persistent store). When I remove the persistent
store from the context, I am getting errors like this:
[...]
This has been included to remove the object as a KVO observer, and
refresh a lazy property, but actually, the notification also arises
when I just use this
-(void)removeCollectionsObject:(KnowledgeCollection *)anObject {
[self willChangeValueForKey:@"collections"];
[[self primitiveValueForKey:@"collections"] removeObject:anObject];
[self didChangeValueForKey:@"collections"];
}
If I remove the method altogether, the exception does not get raised.
So I'm confused, because I would have thought that the implementation
above would be pretty much the same as what Core Data would do
internally if the method is not included. Apparently it is doing
other things to
avoid the exception, but what?
Hi Drew,
When writing a custom to-many accessor in an NSManagedObject subclass,
you need to invoke the correct KVO -will/didChange methods
These are:
-[NSManagedObject willChangeValueForKey:withSetMutation:usingObjects:]
-[NSManagedObject didChangeValueForKey:withSetMutation:usingObjects:]
So your method should change to:
-(void)removeCollectionsObject:(KnowledgeCollection *)anObject {
NSSet *removedObjects = [[NSSet alloc] initWithObjects:&anObject
count:1];
[self willChangeValueForKey:@"collections"
withSetMutation:NSKeyValueMinusSetMutation usingObjects:removedObjects];
[[self primitiveValueForKey:@"collections"] removeObject:anObject];
[self didChangeValueForKey:@"collections"
withSetMutation:NSKeyValueMinusSetMutation usingObjects:removedObjects];
[removedObjects release];
}
Hope this helps, there's a full example in the programming guide:
http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/CoreData/Articles/cdAccessorMethods.html#/
/apple_ref/doc/uid/TP40002154-SW6
Jonathan
http://espresso-served-here.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