• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Design Question: Where to observe using KVO?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Design Question: Where to observe using KVO?


  • Subject: Design Question: Where to observe using KVO?
  • From: Patrick Mau <email@hidden>
  • Date: Thu, 10 Jul 2008 18:02:08 +0200

Hallo everyone

I would appreciate if someone wants to comment on my conceptual design
question:

I have developed a custom NSView displaying a pie chart. The data is
an NSMutableArray containing NSMutableDictionary instances for the pie
segments (a standard NSArrayController instance).

Each segment consists of a label, color, tooltip and value.

My first approach was to keep the data array in the application
controller and implement NSView selectors like:

> - (int)addSegmentWithValue:(NSValue *)value withLabel:(NSString *)label;
> - (void)removeSegmentAtIndex:(int)index;
> - (void)setLabel:(NSString *)label forSegmentAtIndex:(int)index;
> - (void)setTooltop:(NSString *)toolTip forSegmentAtIndex:(int)index;
> - (void)setValue:(NSValue *)value forSegmentAtIndex:(int)index;

... you get the idea

I would observe the data array in the controller and send messages
like the above to the view instance.

Pros:
- The logic is kept in the controller
- The view's data model is hidden in the implementation.
- Updates to the view are completely independent (flexible)

Cons:
- Segment data is duplicated in the view and the model/controller part
- I have to ensure the view is updated properly using setNeedsDisplay:
- Lots of messages have to be sent to the view instance
- A lot of glue code implemented in the controller


My second approach was to let the pie chart view observe the whole
array. It would also add itself as an observer on addition or removal
of array elements. There's only one line of code in the controller
registering the view.

Pros:
- Fewer lines of code, the view observes the array
- Messages are sent automatically to the view using KVO
- The view's data is always consistent

Cons:
- The view's data model is exposed to the outside world
- changes of the view's functionality would be harder to implement
- Lots of messages like '[self setNeedsDisplay:YES]'


Flexibility:

- How would I prevent updates ?
  (lock segments, prevent removal, etc.)

- How would I connect segments
  (segment 'A' and 'B' always cover 33%)?

- How would I use animation while data might change under me?
  (stop observing? that would be inconsistent)


This post is getting too long already, sorry.

Where would you observe changes to data?

On the one hand the view needs consistent data, but in my opinion the
application's data should be separate from the data hosting the
appearance of the view.

Thanks a lot for reading
Patrick

PS: I have been silently reading this list for a few weeks now, I
really enjoy it!


_______________________________________________

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

  • Follow-Ups:
    • Re: Design Question: Where to observe using KVO?
      • From: "I. Savant" <email@hidden>
  • Prev by Date: Re: Why can't I name a property `tag'?
  • Next by Date: Re: Any good advice for naming parameters of init methods?
  • Previous by thread: Re: Printing a Webview
  • Next by thread: Re: Design Question: Where to observe using KVO?
  • Index(es):
    • Date
    • Thread