• 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: Ken Thomases <email@hidden>
  • Date: Fri, 11 Jul 2008 01:34:17 -0500

On Jul 10, 2008, at 4:55 PM, Patrick Mau wrote:

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?

How about a dictionary whose keys are NSValues representing the objects? For each object, the value from the dictionary would be an NSMutableArray whose elements are the key paths being observed on that object.


	// ...
	NSValue* objectKey = [NSValue valueWithNonretainedObject:object];
	NSMutableArray* keyPaths = [observers objectForKey:objectKey];
	if (!keyPaths)
	{
		keyPaths = [NSMutableArray array];
		[observers setObject:keyPaths forKey:objectKey];
	}
	[keyPaths addObject:keyPath];


Cheers, Ken _______________________________________________

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: Patrick Mau <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>)
 >Re: Design Question: Where to observe using KVO? (From: Patrick Mau <email@hidden>)

  • Prev by Date: Re: JDBC Frameworks?
  • Next by Date: Re: JDBC Frameworks?
  • 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