Re: KVO and Core data
Re: KVO and Core data
- Subject: Re: KVO and Core data
- From: Keary Suska <email@hidden>
- Date: Mon, 9 Aug 2010 08:20:36 -0600
On Aug 9, 2010, at 4:51 AM, Gideon King wrote:
> Hi all,
>
> I have a KVO registration like this:
>
> [self addObserver:self forKeyPath:@"toOne.attribute" options:0 context:NULL];
>
> I establish this on -awakeFromInsert or -awakeFromFetch, and have the corresponding removeObserver called on -willTurnIntoFault.
>
> The problem is that when I do a Save As on my document, it migrates the persistent store, which appears to cause the object at the destination of the toOne relationship to be turned into a fault before the object that registered as an observer is. Now when I check the observationInfo for the object that is being faulted (at the end of the toOne relationship), it has itself registered as an observer for "attribute".
>
> I guess this means that behind the scenes, the KVO establishes observation information automatically at every level of a key path (something I hadn't realized before, but which appears to make sense).
Yes.
> Then something behind the scenes tries to access the attribute on an object that has been turned into a fault, and invalidated, and everything comes crashing down.
The trace shows that the destination has been invalidated before your object is faulted, and so the exception is raised when it tries to remove observation. Invalidated object references should be rare--I would expect Core Data to "clean up" relationships when a MOC is reset but that does not appear to be the case (at least not all the time). Have you re-looked at the issues around the question you asked back in April: http://www.cocoabuilder.com/archive/cocoa/284398-invalidated-managed-objects.html ?
If that doesn't help, the easiest workaround I can think of is have the destination observe its attribute, and then call a method on all objects from the inverse to-many.
> So I have two questions:
>
> 1. Is it the "right way" for me to be adding observation on the awake methods and removing it on willTurnToFault?
For cross-relationship KVO, generally yes.
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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