2014-09-24 4 views
3

옵저버를 제거하는 데 문제가 있습니다. 이벤트가, LISTOFITEMS가 removeAllObservers observer 제거되지 않았습니다.

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

관찰되고 있지만 어떤 시점에서, 나는 항목 1 키를 업데이트하려면 다음

는 데이터 구조 처음

listOfItems 
    Item 1 
     Key:Value 
    Item 2 
     Key:Value 
에도 removeAllObservers 후 화재로 나타납니다 값 쌍을 관찰하지 않고 아이템 1에서 관찰자를 제거합니다.

[refToItem1 removeAllObservers];

는 항목 1

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

[refToItem1 updateChildValues:testData]; 

의 사전을 업데이트를 진행하지만, 관찰자 ​​이벤트는 여전히 refToItem1 요소에 대해 발생합니다.

무엇이 누락 되었습니까?

수정 개체를 관찰하는 것은 해당 개체에 암시 적으로 설정되어있는 경우에만 제거 할 수있는 것처럼 보입니다. 즉, 객체에 대해 관찰을 설정하면 해당 관찰을 제거 할 수 있습니다. 그러나 관찰되는 첫 번째 객체의 하위 객체에서는 제거 할 수 없습니까?

답변

3

정확하게 이해한다면이 문제가 발생했습니다. 루프에서 매번 덮어 쓰기 때문에 iVar에 refToItem1을 저장하면 작동하지 않습니다.

내 솔루션은 하위 Firebase 참조를 배열에 저장 한 다음 모든 옵저버를 제거 할 때 해당 배열을 반복합니다.

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

    for (Firebase *ref in self.childObservers) { 
     [ref removeAllObservers]; 
    } 
} 
: 다음
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 
    }]; 
}]; 

나중에 관찰자를 제거합니다
관련 문제