• 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
Re: Design Question: Where to observe using KVO?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Design Question: Where to observe using KVO?


  • Subject: Re: Design Question: Where to observe using KVO?
  • From: Patrick Mau <email@hidden>
  • Date: Thu, 10 Jul 2008 23:55:11 +0200

I. Savant wrote:
>> Check out this page:
>>
>> http://homepage.mac.com/mmalc/CocoaExamples/controllers.html
>>
>
>   I should also mention that you should approach these examples like this:
>
> 1 - Read the Cocoa Bindings documentation (including all the Key Value
> Coding / Key Value Observing related documentation).
> 2 - Examine the examples given above.
> 3 - Go back over the documentation!!! Seeing a working example makes a
> second perusal of the docs much more fruitful.

Thanks a lot for your remarks, I really appreciate it. My basic
implementation already works!

I have built a master-detail window, using an NSTable view for label
and amount. My PieChartView is observing the array like this:

> - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
> 			change:(NSDictionary *)change context:(void *)context
> {
>     NSLog(@"observeValueForKeyPath:%@ ofObject:%p %@", keyPath, object, change);
>
>     int i;
>     NSArray *changedObjects;
>     int changeType = [[change valueForKey:NSKeyValueChangeKindKey] intValue];
>
>     switch (changeType) {
> 	case NSKeyValueChangeInsertion:
> 	    changedObjects = [change valueForKey:NSKeyValueChangeNewKey];
> 	    for (i = 0; i < [changedObjects count]; i++) {
> 		[self observeObject:[changedObjects objectAtIndex:i] withKeyPath:@"label"];
> 		[self observeObject:[changedObjects objectAtIndex:i] withKeyPath:@"amount"];
> 	    }
> 	    break;

This is an excerpt of the code executed whenever a new row is added to
the NSTableView. I observe the new NSDictionary provided by
NSArrayController. Deleting multiple rows also works.

I'd like to show you one ugly detail. I put all observed objects,
including their keyPath in a local NSDictionary, because I have to
remove the observers on deletion of a table row. The 'observeObject'
looks like that:

> - (void)observeObject:(id)object withKeyPath:(NSString *)keyPath {
>     NSLog(@"start observing %p with keypath %@", object, keyPath);
>
>     [object addObserver:self forKeyPath:keyPath
> 		options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
>
>     [observers setValue:object forKeyPath:[NSString stringWithFormat:@"%p.%@", object, keyPath]];
> }

In my opinion, this is as ugly as it can possibly get.

I'm using the address of the observed object and append the keypath to
create a pseudo key for each observed instance.

I don't like that at all, but how would I be able to remove all
observers when I have to clean them up later?

Is there a better approach instead of observing every property of all
array entries?

Patrick

_______________________________________________

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: Ken Thomases <email@hidden>
References: 
 >Design Question: Where to observe using KVO? (From: Patrick Mau <email@hidden>)
 >Re: Design Question: Where to observe using KVO? (From: "I. Savant" <email@hidden>)
 >Re: Design Question: Where to observe using KVO? (From: "I. Savant" <email@hidden>)

  • Prev by Date: Re: JDBC Frameworks?
  • Next by Date: Re: How do I copy a view?
  • Previous by thread: Re: Design Question: Where to observe using KVO?
  • Next by thread: Re: Design Question: Where to observe using KVO?
  • Index(es):
    • Date
    • Thread