• 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: KVO one-step listening but two-step notifying?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: KVO one-step listening but two-step notifying?


  • Subject: Re: KVO one-step listening but two-step notifying?
  • From: Michel Schinz <email@hidden>
  • Date: Fri, 23 Dec 2005 10:56:14 +0100

Le 23 déc. 05 à 03:23, Hamish Allan a écrit :

[...]

Thank you; I should be able to make that work fine for my situation. But what about the more general case -- for example what if you were writing an object which merged the contents of two arrays from other objects, so you would want to notify of changes to your array if either of the object's arrays changed?

- (id)mergedArray
{
return [[_object1 individualArray] arrayByAddingObjectsFromArray: [_object2 individualArray]];
}

You can observe changes to each of those individualArrays, but by the time observeValueForKeyPath:... is called it's too late to call willChangeValueForKey:@"mergedArray" because -mergedArray will already be providing the new contents.

It's a problem I often encounter in my code, and the solution I'm using is to have an instance variable to hold the result of the function (the merged array in your case). Then, in observeValue... I simply call the setter for that variable (which is usually private), and this ensures that KVO is handled correctly (and automatically). So in your case I would write something like (warning: untested code):

- (id)mergedArray {
return _mergedArray;
}

- (void) observeValueForKeyPath... {
// one of the two arrays changed
[self setMergedArray:[[_object1 individualArray] arrayByAddingObjectsFromArray:[_object2 individualArray]]];
}

- (void) setMergedArray:(NSArray*)newArray;
{
// ... standard setter
}

I'm a bit tired of writing the same thing over and over just to make sure that my properties are KVO-compliant, that said, and I'd be interested in a less verbose solution if there is one.

Michel.


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
  • Follow-Ups:
    • Re: KVO one-step listening but two-step notifying?
      • From: Hamish Allan <email@hidden>
References: 
 >KVO one-step listening but two-step notifying? (From: Hamish Allan <email@hidden>)
 >Re: KVO one-step listening but two-step notifying? (From: mmalcolm crawford <email@hidden>)
 >Re: KVO one-step listening but two-step notifying? (From: Daniel Jalkut <email@hidden>)
 >Re: KVO one-step listening but two-step notifying? (From: Hamish Allan <email@hidden>)

  • Prev by Date: Re: KVO one-step listening but two-step notifying?
  • Next by Date: NSOpenPanel runModalForTypes: argument
  • Previous by thread: Re: KVO one-step listening but two-step notifying?
  • Next by thread: Re: KVO one-step listening but two-step notifying?
  • Index(es):
    • Date
    • Thread