gpt4 book ai didi

swift - Firebase,我如何返回句柄以便我可以调用 removeObserver?

转载 作者:搜寻专家 更新时间:2023-11-01 07:18:38 27 4
gpt4 key购买 nike

这是我的观察事件代码:

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
})

如何在调用特定观察者时检索句柄,以便在 viewDidDissapear 时将其删除?

最佳答案

当您调用 observe 时它向已注册的观察者返回一个句柄。

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

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

然后:

query.removeObserverWithHandle(handle)

关于swift - Firebase,我如何返回句柄以便我可以调用 removeObserver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40346533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com