2014-09-24 78 views
3

移除觀察者時出現問題;事件甚至出現removeAllObservers火後removeAllObserver觀察者未被移除

這裏的數據結構

listOfItems 
    Item 1 
     Key:Value 
    Item 2 
     Key:Value 

最初,LISTOFITEMS正在觀察

[refToListOfItems observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) { 
    NSLog(@"responding to value change in the list of items"); 
}]; 

但在某些時候,我想更新項目1項:值因此我從項目1刪除觀察者1

[refToItem1 removeAllObserver];

然後繼續更新項目1

NSDictionary *testData = @{ 
          @"newKey": @"newValue" 
          }; 

[refToItem1 updateChildValues:testData]; 

的字典,而是觀察者事件仍解僱的refToItem1元素。

我錯過了什麼?

編輯 看來只有在對象上隱式設置觀察對象時才能刪除對象。即如果您在對象上設置觀察,則可以刪除該觀察。但它不能被刪除的第一個對象的子對象被觀察?

回答

3

我自己遇到了這個問題,如果我正確理解你的話。在iVar中存儲refToItem1將無法​​正常工作,因爲它在循環中每次都被覆蓋。

我的解決方案是將孩子Firebase引用存儲在數組中,然後在我想要刪除所有觀察者時遍歷該數組。

例如

self.parentRef = [[Firebase alloc] initWithUrl:@"url"]; 

NSMutableArray *childObservers = [[NSMutableArray alloc] init]; 

[self.parentRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) { 
    Firebase *childRef = [ref childByAppendingPath...]; 

    [childObservers addObject:childRef]; 

    [ref observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *childSnapshot) { 
     ... observations 
    }]; 
}]; 

再後來刪除觀察員:

- (void)stopObserving { 
    [self.parentRef removeAllObservers]; 

    for (Firebase *ref in self.childObservers) { 
     [ref removeAllObservers]; 
    } 
}