Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6935503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:06:41+00:00 2026-05-27T12:06:41+00:00

In line with Apple’s own recommendations, when setting KVC/KVO compliant accessors manually, one should

  • 0

In line with Apple’s own recommendations, when setting KVC/KVO compliant accessors manually, one should include BOTH KVO methods willChange and didChange. This is what I have done in all my manual accessor methods.

However, observeValueForKeyPath:ofObject:change:context gets called for each half of the KVC methods (will and did) with exactly the same dictionary contents.

When registering an observer using the option: NSKeyValueObservingOptionPrior the observer still gets called twice – once for each half – and, again, with identically the same dictionary contents, save only the difference that the key ‘notificationIsPrior’ is included in the dictionary.

Now, when KVO is used to alter ‘CPU-expensive’ attributes – like changing a colour or redrawing a large and elaborate design, it makes sense only to act on the ‘didChange’ and ignore (or at least separate out) the ‘willChange’. In the past, I have achieved this by converting the key string into an enum list element that returns a left-shifted ‘1’ and used this digit to set a flag in a 32 or 64 bit integer on receipt of the first call and when the flag is reset on the second, I execute the CPU-intensive operation(s).

However, it strikes me that this is a non-trivial overhead to implement for every case. Does anyone have any other ‘preferred’ way of differentiating between the callback for ‘willChange’ and that for ‘didChange’ without allowing the same processing to be done twice?

I have scoured Apple’s own documentation and this help group copiously for alteranatives but Apple’s own doc doesn’t actually go in to much detail on the subject and several people in this group have also wrestled with a similiar concern. In neither instance has a definitive solution been offered. If anyone knows of a better way – other than dodging the ‘willChange’ using alternating flags – I’d be very grateful. (Why couldn’t Apple just include a ‘phase’ key in the change dictionary???)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:06:42+00:00Added an answer on May 27, 2026 at 12:06 pm

    I think this is what you were getting at in the comments, but for the benefit of future visitors:

    If you want to tell whether a callback is “before” or “after” you can look for the NSKeyValueChangeNotificationIsPriorKey key in the change dictionary. If it’s a prior notification, this key will be equal to [NSNumber numberWithBool: YES] (incidentally the dictionary will also not contain a value for the NSKeyValueChangeNewKey) The presence/value of NSKeyValueChangeNotificationIsPriorKey is authoritative, so if you’re seeing it when you’re not expecting to, you might be getting double callbacks.

    If you’re getting double callbacks it may be, as it sounds like it was in VectorVictors case, that the runtime is firing them AND you’re firing them. If you plan to call will/didChangeValueForKey: to manage your KVO notifications manually, (and you don’t want double notifications,) you should implement the following class method:

    + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)theKey {
    
        BOOL automatic = NO;
        if ([theKey isEqualToString:@"propertyYourePlanningToManageYourself"]) {
            automatic = NO;
        } else {
            automatic=[super automaticallyNotifiesObserversForKey:theKey];
        }
        return automatic;
    }
    

    This is described in detail in Apple’s Key-Value Observing Programming Guide.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to Apple's guide line, an iPhone app's launch image/splash screen should be a
I see this line on one of Apple samples... if (&ADBannerContentSizeIdentifierPortrait != nil) what
Is there a way to compile apple script code on the command line from
this line of code is for the navigation bar of Apple.com #globalheader #globalnav[class*=nosearch] {
I'm trying to write a command line script to access Apple SDK docsets (like
The package Apple provides to install the command line tools with Xcode 4.3 is
With the release of Apple's Command Line Toolkit for Xcode, I am trying to
In my app I'm using Apple's hdiutil command line utility to create compressed disk
I suppose it's not strictly in line with Apple guidelines but I guess it
I copied this method, line by line from the apple dev library and getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.