Search code examples
iosobjective-ckey-value-observing

Observing values for a primitive pointer


I have a class with a property

@property (nonatomic) double* myDoubles

This property has 3 doubles in it

myDoubles[0]; //0.02
myDoubles[1]; //0.42
myDoubles[2]; //0.99

If the values change, I'd like the following method to be called

[self setNeedsDisplay];

I tried using FBKVOController, but that didn't work..

_observer = [FBKVOController controllerWithObserver:self];
[_observer observe:self
           keyPath:@"myDoubles"
           options:NSKeyValueObservingOptionNew
            action:@selector(setNeedsDisplay)];

I don't want to start an NSTimer and just check for changes.


Solution

  • This is not possible.

    Notifications work because the code making changes does so through some method that knows to notify listeners of the change. If that same code were simply to write to the memory location backing the data, the notification would never be triggered.

    What you want to do is simply declare a memory location that code will write to; no notification can happen from this (unless you have very system-dependent support making it possible - a memory watchpoint - and then your question changes significantly. Such support, when available, is very limited and not of good generic value).