The KVO Race
The KVO Race
- Subject: The KVO Race
- From: Gordon Apple <email@hidden>
- Date: Tue, 03 Jun 2008 11:53:43 -0500
This probably isn't news to most programmers, but since I've run into it
twice now, I thought it was worth a post.
I have had cases where my program worked correctly sometimes, sometimes
not. In two cases, I have traced the problem to KVO race conditions. I
will describe one.
The displayController observes a NSIndexPath that determines where the
presentation is in the presentation sequence. The observation extracts the
current Shot (display object) and it's current Layer, editLayer (I.e., a
draw list of shapes). The displayEditController observes the
displayController's editLayer. Both observations are set up in the same Nib
in "awakeFromNib".
The problem was that if displayEditController was set up AFTER the
displayController, it didn't get to observe that editLayer had changed from
nil (its initialized value) and didn't start observing the shape observation
parameters for undo (a la the Sketch example). The next time editLayer
changed, the displayEditController tried to stop observing editLayer
parameters for which it hadn't started observing in the first place. This
was solved by testing for a nil displayController editLayer in
dissplayEditController's "awakeFromNib" and starting parameter observation
if it was not nil.
Simple mistake, but it took a month or two before I figured out what was
going on. It was a simple race condition between KVOs set up in the two
controller's "awakeFromNib". I suppose any idiot should have caught this,
but it was just one of those OSIFATs (Oh S---, I Forgot About That) that I
didn't anticipate.
I hope this example helps someone encountering similar problems. KVO is
great stuff, but it is subject to the same kinds of caveats as other
programming techniques. Don't assume anything -- always test any
environmental conditions that could have any possibility of impacting your
desired results.
_______________________________________________
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