我正在使用 KVO 观察核心数据值,但是当我删除观察者时,我收到以下消息:
无法从中删除关键路径“flightname.value”的观察者,因为它未注册为观察者。
下面的代码调用了两次。第一次 _selectedFlight 为 null,因此不会触发 removeObserver。第二次我想删除,第一次分配了什么。观察者地址不变。
//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];
在这里你可以看到observationInfo的内容和调用removeObserver之前的一些细节:
(lldb) po oi (id) $1 = 0x06b42d80 (上下文: 0x0, 属性: 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 "; 注册 = "0x6e60700 "; sta = "0xc81fb90 "; std = "0xc8203d0 "; todc = "0x6b293e0 "; })