RemoveAllObservers observer not removed

there is a problem with the removal of the observer; event seems to fire even after removing AllObservers

Here's the data structure

listOfItems
    Item 1
        Key:Value
    Item 2
        Key:Value

      

Initially observed listOfItems

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

      

but at some point i want to update Item 1 Key: Value pair without observation, so I remove the observer from Item 1

[refToItem1 removeAllObservers];

then go to update the dictionary item 1

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

[refToItem1 updateChildValues:testData];

      

But the observer event is still fired for refToItem1.

What am I missing?

EDIT It looks like watching an object can only be removed if it is implicitly set on that object. those. if you set an object observation, this observation can be deleted. But it cannot be removed on the children of the first observable?

+3


source to share


1 answer


I ran into this question if I understand you correctly. Storing refToItem1

in iVar won't work as it gets overwritten every time in the loop.

My solution was to store Firebase child references in an array and then iterate over that array when I want to remove all observers.

eg.



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
    }];
}];

      

Then later to remove the observers:

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

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

      

+3


source







All Articles