Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove firebase observer?

I have tableView and refreshControl. I want that tableView's data reload only when I pull-to-refresh. I've made the function that reload data, but it works always.

Data reload after pull-to-refresh, but also observe update tableView. How to remove it? I want to make observe, reload data and remove observe (remove connection).

func reloadTable() {
    let ref = Database.database().reference()
    let userID = Auth.auth().currentUser?.uid
    postData.removeAll()

    var databaseHandle: DatabaseHandle?
    databaseHandle = ref.child("doctors").child(userID!).observe(.childAdded) { 
        (snapshot) in
        if let actualPost = snapshot.value as? String {
            self.postData.append(actualPost)
            self.table.reloadData()
        }
    }

    // I tried this:
    ref.removeAllObservers()

    // And this as well:
    ref.removeObserver(withHandle: databaseHandle)
}
like image 765
Igor Sorokin Avatar asked Oct 18 '25 05:10

Igor Sorokin


1 Answers

You need

let current = ref.child("doctors").child(userID!) 
 current.observe ///
current.removeAllObservers()

if you need to removeObservers you need to go deep as you add childs , as removeAllObservers for parents doesn't remove them for childs

like image 96
Sh_Khan Avatar answered Oct 20 '25 18:10

Sh_Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!