LandonSchropp
LandonSchropp

Reputation: 10274

Triggering a property update with KVO

I'm having a hard time manually triggering a property update with key-value-observing. Here's a contrived example to illustrate my problem:

Bar.h

#import <Foundation/Foundation.h>

@interface Bar : NSObject
{
    NSString *test;
}

@property (nonatomic, retain) NSString *test;

-(void) updateTest1;
-(void) updateTest2;

@end

Bar.m

#import "Bar.h"

@implementation Bar

@synthesize test;

-(id) init
{
    if (self = [super init])
    {
        self.test = @"initial";
    }
    return self;
}

-(void) updateTest1
{
    self.test = @"updating 1";
}

-(void) updateTest2
{
    NSString *updateString = @"updating 2";
    [updateString retain];
    test = updateString;
    [self didChangeValueForKey:@"test"];
}

@end

Foo.h

#import <Foundation/Foundation.h>

@interface Foo : NSObject

@end

Foo.m

#import "Foo.h"

@implementation Foo

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"something changed!");
}

@end

main.m

#import <Foundation/Foundation.h>
#import "Foo.h"
#import "Bar.h"

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Bar *bar = [Bar new];
    Foo *foo = [Foo new];

    [bar addObserver:foo forKeyPath:@"test" options:0 context:nil];

    [bar updateTest1];
    [bar updateTest2];

    [pool drain];
    return 0;
}

The program returns:

2011-08-09 03:57:52.630 Temp[5159:707] something changed!
Program ended with exit code: 0

Why is didChangeValueForKey: not triggering the observer's observeValueForKeyPath:ofObject:change:context: event? Does this method not work like I think it does?

Upvotes: 2

Views: 1827

Answers (2)

hooleyhoop
hooleyhoop

Reputation: 9198

It is not triggering the notification because you forgot the corresponding

[self willChangeValueForKey:@"test"];

that must always be paired with didChangeValueForKey:

Upvotes: 6

NSKeyValueObservingOptions

These constants are passed to addObserver:forKeyPath:options:context: and determine the values that are returned as part of the change dictionary passed to an observeValueForKeyPath:ofObject:change:context:. You can pass 0 if you require no change dictionary values.

enum {

NSKeyValueObservingOptionNew = 0x01,

NSKeyValueObservingOptionOld = 0x02,

NSKeyValueObservingOptionInitial = 0x04,

NSKeyValueObservingOptionPrior = 0x08

}; typedef NSUInteger NSKeyValueObservingOptions;

try like this

[bar addObserver:foo forKeyPath:@"test" options:NSKeyValueObservingOptionNew context:nil];

change this code

-(void) updateTest2
{
    NSString *updateString = @"updating 2";
    [updateString retain];
    test = updateString;
    [self didChangeValueForKey:@"test"];
}

to

-(void) updateTest2
{


    self.test = @"updating 2"//note here

    [self didChangeValueForKey:@"test"];
}

Upvotes: 1

Related Questions