0

I am observing a core data value with KVO, but when I remove the observer, I get the message:

Cannot remove an observer for the key path "flightname.value" from because it is not registered as an observer.

Code below called two times. First time _selectedFlight is null, so removeObserver is not triggered. Second time I want to remove, what first time was assigned. Observer address is not changing.

//Remove old binding.
if (_selectedFlight) {
    id oi = [_selectedFlight observationInfo];
    if (![oi isFault]) {
        //NOTE: In somehow I cant remove it.
        [_selectedFlight removeObserver:self forKeyPath:@"flightname.value"];
    }
}

_selectedFlight = [_selectedRegistration.flights objectAtIndex:(_selectedRegistration.flights.count - indexPath.row - 1)];

//Bind flightname to the Summary View.
[_selectedFlight addObserver:self forKeyPath:@"flightname.value" options:0 context:NULL];

Here you can see the content of the observationInfo and some details before removeObserver is called.:

(lldb) po oi (id) $1 = 0x06b42d80 ( Context: 0x0, Property: 0x6b425e0> )

(lldb) po self (MenuViewController *const) $2 = 0x08136f90

(lldb) po _selectedFlight (Flight *) $3 = 0x06b26770 (entity: Flight; id: 0x6b26ef0 ; data: { date = "0x6b1b8f0 "; destination = "0x6b29140 "; flightname = "0x6b29150 "; id = 1; ladc = nil; loadsheet = nil; origin = "0x6b29160 "; registration = "0x6e60700 "; sta = "0xc81fb90 "; std = "0xc8203d0 "; todc = "0x6b293e0 "; })

János
  • 32,867
  • 38
  • 193
  • 353
  • This could be an indexing issue - just to debug this, add an additional value to the list of observers in each registration and make sure you're fetching the same one you added the observer to. – Stavash Jun 30 '12 at 11:02
  • Can you also log the addresses of _selectedFlight and self right before you remove the observer. Just to be sure you are removing the right observer. – diederikh Jun 30 '12 at 11:07
  • The 0x08136f90 address was not changed meanwhile. The interesting is that if I modify the code, and removeObserver is called right after I added, then no problem, If I am navigating the GUI, and call it later, I get this crash. – János Jun 30 '12 at 11:22

1 Answers1

0

I was passing by this issue in legacy code, and I do the follow :

 if (object && object.observationInfo)
    [object removeObserver:self forKeyPath:@"key"];

Because my code is legacy, I don't know what is causing this issue, but I discovered that checking the observationInfo property fixed it.

Hope helps.

Felipe FMMobile
  • 1,641
  • 20
  • 17