aashish nagar
aashish nagar

Reputation: 59

removeObserverWithHandle does not seems to be working in fireBase iOS

I am fetching the data from firebase and after that removing the observer. But if data gets changes at that location block is still gets called.

Code to fetch data :

  if messageCountDBReference == nil {
         messageCountDBReference = FireBaseReferenceFactory.messageListReference(forSericeId: serviceId)
    }

   messageCountDBHandle = messageCountDBReference!.observe(.value, with: { (dataSnapShot) in
/// data processing 

})

In ViewWillDisappear removing observer.

if messageCountDBHandle != nil {
    messageCountDBReference?.removeObserver(withHandle: messageCountDBHandle!)
    }
    messageCountDBReference = nil
    messageCountDBHandle = nil

Upvotes: 2

Views: 173

Answers (1)

Do2
Do2

Reputation: 1791

You have put the .removeObserver in viewWillDisappear that means that it will be removed just before the view is disappeared. So if you stay in that view for 10 minutes, and 100 changes happen in that Firebase path, it will display all 100 of them.

What you are looking for is .observeSingleEvent that reads data once and then removes the observer.

Upvotes: 2

Related Questions