2012-06-30 122 views
0

我觀察與國際志願者組織核心數據的值,但是當我刪除觀察者,我得到的消息:removeObserver失敗,核心數據

無法從,因爲它刪除觀察員關鍵路徑「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自(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」; })

+0

這可能是一個索引問題 - 只是爲了調試它,在每個註冊中的觀察者列表中添加一個額外的值,並確保您獲取的是您添加觀察者的同一個值。 – Stavash

+0

您還可以在刪除觀察者之前記錄_selectedFlight和自己的地址。只是爲了確保你正在移除正確的觀察者。 – diederikh

+0

0x08136f90地址沒有改變。有趣的是,如果我修改了代碼,並且removeObserver在我添加後立即調用,那麼沒問題,如果我正在導航GUI並稍後調用它,那麼會發生此類崩潰。 –

回答

0

我路過這個問題遺留代碼,我做了如下:

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

因爲我的代碼是舊的,我不知道是什麼原因造成這個問題,但我發現檢查observerInfo屬性修復了它。

希望有所幫助。