0

I had 2 Objects Obj1 and Obj2. Where Obj1 is having a property isLocalChanges(BOOL) to check for any local changes made. I need to observe the change for this isLocalChanges if there is any changes I am setting the value to YES.

I written an observer for this property isLocalChanges in Obj2:

@interface Obj2 : NSObject
{
   Obj1 *sampleObj;
}
@property (nonatomic,retain) Obj1 * sampleObj;

@end

@implementation Obj2
@synthesize sampleObj;
- (id)init
{
    if (self = [super init])
    {
        sampleObj =  [[Obj1 alloc] init];

        [sampleObj  addObserver:self
                forKeyPath:@"isLocalChanges"
                   options:NSKeyValueObservingOptionNew
                   context:nil];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {       
    if ([keyPath isEqualToString:@"isLocalChanges"]) {
            NSLog(@"isLocalChanges Observer");
            if ([[change objectForKey:@"new"] boolValue]) {
                 NSLog("@\n Found Local Changes...");
            }
        }

    }
}

Declaration and usage of isLocalChanges property that I used:

@interface Obj1 : NSObject
    {
       BOOL _isLocalChanges;
    }
    @property (assign) BOOL isLocalChanges;
@end

and in the Obj1.m

did @synthesize isLocalChanges = _isLocalChanges;

And Inside the below method I am setting the value for isLocalChanges property.

-(void) localChangesMade 
{
   self.isLocalChanges = YES;
}

The issue is even if isLocalChanges property changed in Obj1, The method observeValueForKeyPath: is not triggering. At this point I am helpless an unable to find what going wrong.

Can anyone suggest me to resolve this issue...

Monish Kumar
  • 2,788
  • 4
  • 38
  • 54

2 Answers2

0

The object you are allocating is going out-of-scope and will be destroyed as soon as the method returns:

SomeObject *Obj2 =  [[SomeObject alloc] init];

Make it an instance variable instead.

Droppy
  • 9,691
  • 1
  • 20
  • 27
0

You are using the identifier Obj2 for three different things (a) as a class name (b) as an instance variable/property and (c) as a local variable. This is not good.

It is probably the third use which is your immediate problem, it is probably meant to be a use of (b). Instead of:

if (self = [super init])
{
    SomeObject *Obj2 =  [[SomeObject alloc] init];

you probably want:

if (self = [super init])
{
    Obj2 =  [[SomeObject alloc] init];

etc. The SomeObject *Obj2 local variable's lifetime is only the body of the if.

However that fix has you assigning an instance of type SomeObject to a property of type Obj1, so you may have a problem there as well.

Finally you never show the declaration or use of isLocalChanges, so there is no indication that you're actually assigning to this property using its setter - and if you're not then there won't be any KVO notifications.

CRD
  • 52,522
  • 5
  • 70
  • 86
  • I am assigning like self.isLocalChanges = YES; in the Obj1 class. because this is a synthesized property. – Monish Kumar Jan 05 '15 at 13:10
  • I edited the post, check it and provide me with some suggestions where I am going wrong – Monish Kumar Jan 05 '15 at 13:58
  • Have you checked that you are calling `localChangesMade` on the *same* instance of `Obj1` that is referenced by `sampleObj`? (You can use `NSLog` and the `%p` format to help here.) – CRD Jan 05 '15 at 17:11