Problems with loose ViewController coupling and KVO to-many relationships
Problems with loose ViewController coupling and KVO to-many relationships
- Subject: Problems with loose ViewController coupling and KVO to-many relationships
- From: "Jonathan del Strother" <email@hidden>
- Date: Tue, 30 Sep 2008 20:19:05 +0100
Heya
I'm struggling a bit with NSViewControllers. My app displays a list
of widgets. I have a Widget model, a WidgetView view (an NSView
subclass), and a WidgetViewController (an NSViewController subclass).
All of those have a one-to-one mapping - for any given
WidgetViewController, it has a representedObject pointing to the
Widget, and a view pointing to the WidgetView. The user can
add/remove/reorder the widgets at any time.
So, there's an array of Widgets, which the window controller observes,
and whenever that array changes, it creates/deletes
WidgetViewControllers (and inserts WidgetView subviews) to match. So
far so good.
(In fact, if I give the Widget model a strong relationship to the
WidgetViewController (ie the Widget retains its view controller), it
works perfectly. Unfortunately, that totally ignores the nice fluffy
ideal of MVC separation.)
There's a design flaw here somewhere, but I can't quite figure out
what it is. It's highlighted by trying to reorder widgets. With KVC
to-many relationships, there's no atomic way of moving an object from
one position in an array to another : you remove the object, then
re-insert it at the new location. But when the object is
(temporarily) removed from the array, the window controller gets an
observeValueForKeyPath saying it's been deleted, and so releases the
corresponding WidgetViewController, the view gets deallocated, and so
on. I'll then get a second observeValueForKeyPath saying that an
object has been inserted, recreate the WidgetViewController, re-insert
the view, etc etc.
This seems pretty wrong to me. I can see a few workarounds (perhaps
storing the kvo notifications until the end of the current runloop so
I can add them together and figure out what was really a deletion and
what was just a reordering), but they all seem pretty ugly and
heavyweight.
How is this usually handled? How do you managed your view controller
lifetimes w.r.t. the model lifetime?
Thanks for reading this far, any suggestions would be greatly appreciated
-Jonathan
_______________________________________________
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