Question

Not sure I am asking this right but I'll give it a try anyway.

I have a program which invokes this little bit of code...

[localCalculateTimeFiles addObserver:self
       forKeyPath:@"arraystatuscounter"
          options:NSKeyValueObservingOptionNew
          context:NULL];

Eventually localCalculateTimeFiles goes away and I'd like to removeObserver the current bit of code (to put it crudely). If I do nothing and localCalculateTimesFiles goes away without me doing the proper removeObserver, the runtime throws an exception. I think it kind of used to be more of a warning but now the program abends.

Let's say I get all of the object pointers right and to the right place at the right time. What I'd like to do is have localCalculateTimeFiles remove the observer on the way down (so to speak) when it is being deallocated.

It seems that according to the Apple docs, I can add a method to localCalculateTimeFiles that looks like this to do the job...

- (void)finalize {
    if (log_file != NULL) {
        fclose(log_file);
        log_file = NULL;
    }
    [super finalize]; 
}

I'd replace the logic for dealing with log_file with new logic which would unregister the observer. Clearly, I'd have to make sure I have all of my pointers right to make sure that the removeObserver method has the right items to operate against. Give I do that properly, does this make sense or am I totally off?

No correct solution

OTHER TIPS

I don't think finalize is the correct place for this.

The best place to unregister for observations is in your dealloc method, which is called just before the object is deleted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top