Re: KVO query
Re: KVO query
- Subject: Re: KVO query
- From: Quincey Morris <email@hidden>
- Date: Wed, 13 Aug 2014 15:41:39 -0700
On Aug 13, 2014, at 14:53 , Jonathan Mitchell <email@hidden> wrote:
> At one point i need to invoke a manual KVO notification like so:
>
> [submission willChangeValueForKey:@“status”];
> [submission didChangeValueForKey:@“status”];
>
> This raises like so:
>
> Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot update for observer <NSKeyValueObservance 0x608000ac21b0> for the key path "status.name" from <Submission 0x6080003aa800>, most likely because the value for the key "status" has changed without an appropriate KVO notification being sent. Check the KVO-compliance of the Submission class.’
This is one of those infuriating errors that is hard to make sense of. Try thinking about it literally.
Because you’re observing the “status.name” path from ‘submission’, the observer is *also* observing the “name” path from ‘submission.status’**. In your scenario, the originally observed ‘status’ instance (call it X) is no longer the current ‘status’ instance at willChange time (it’s now Y, say), and in general it might not be the same as that at didChange time (Z, say). Even if Y == Z, there’s been a non-KVO-compliant change in ‘status’ prior to the notification being sent.
Now, we do this quite often, without problems, but I’d suggest that’s only in cases where the “temporarily non KVO compliant” change is for the *last* key of the path — in which case the last object isn’t being observed, just because it’s the last key.
So, in the case of a non-KVO-compliant change to the value of a non-terminal key path object, the non-KVO-compliance may not be tolerated by the frameworks. Hence this error message.
What I suggest you try is to make two properties: “currentStatus” and “observableStatus”. The second of these would be updated KVO compliantly (i.e. via its setter) when you know that the first has changed and the change should be propagated (i.e. in the places where you now trigger the willChange/didChange notification manually). Code in the ‘submission’ class can use ‘self.currentStatus’ to get or set the most recent status object; observers would observe “observableStatus”, of course.
** Because there’re “really” no such thing as a key path observation. It’s “really” a chain of object/key observations.
_______________________________________________
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
References: | |
| >KVO query (From: Jonathan Mitchell <email@hidden>) |