Firebase,如何返回句柄,以便我可以调用removeObserver? [英] Firebase, how do I return a handle so that I can call removeObserver?

查看:143
本文介绍了Firebase,如何返回句柄,以便我可以调用removeObserver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  let databaseRef = FIRDatabase.database()。reference()
let查询= databaseRef.child(palettes)。queryOrdered(byChild:UserDefaults.standard.string(forKey:UserTypeState)!)。queryEqual(toValue:modifiedColor.hexValue())
//将值更改为颜色的值。

query.observe(.childAdded,with:{(snapshot)in
let URL = snapshot.childSnapshot(forPath:URL)。value as!String
self。 URLArrayString.append(URL)

self.collectionView?.reloadData()//在数字和所有URL被提取后重新加载数据
self.noResultsLabel.isHidden = true
})

当我调用特定的观察者时,如何检索句柄, viewDidDissapear? 当您调用 observe 它返回注册观察者的句柄。 b
$ b

您将此句柄传递给 removeObserverWithHandle()以删除观察者。



<$ ($。$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $)



  query.removeObserverWithHandle (处理)


Here's my observe event code:

let databaseRef = FIRDatabase.database().reference()
            let query = databaseRef.child("palettes").queryOrdered(byChild: UserDefaults.standard.string(forKey: "UserTypeState")!).queryEqual(toValue: modifiedColor.hexValue())
            // Change the value to the value of the color.

            query.observe(.childAdded, with: { (snapshot) in
                let URL = snapshot.childSnapshot(forPath: "URL").value as! String
                self.URLArrayString.append(URL)

                self.collectionView?.reloadData() //Reloads data after the number and all the URLs are fetched
                self.noResultsLabel.isHidden = true
            })

How do I retrieve the handle when that particular observer is called so that I can remove it when viewDidDissapear?

解决方案

When you call observe it returns a handle to the registered observer.

You pass this handle to removeObserverWithHandle() to remove the observer.

let handle = query.observe(.childAdded, with: { (snapshot) in
    ....
})

And then:

query.removeObserverWithHandle(handle)

这篇关于Firebase,如何返回句柄,以便我可以调用removeObserver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆