gpt4 book ai didi

ios - 如果未显示 View Controller (导航 Controller 的后堆栈),则不会调用 Firestore addSnapshotListener

转载 作者:行者123 更新时间:2023-11-30 11:46:07 24 4
gpt4 key购买 nike

我想了解如何让 addSnapshotListener 接收更新,即使未显示 View Controller 。

假设我在 ViewController A 上调用 addSnapshotListener,然后一段时间后我转到 View Controller B。

在 ViewController B 中,我将一些数据添加到 ViewController A 的 addSnapshotListener 正在监听的集合中。然而,从 ViewController B 返回到 ViewController A 后,似乎没有查询到新元素。

我考虑过向 ViewController A 的 viewDidAppear 调用 addSnapshotListener 以便更新它,但这是否需要我之前删除所有监听器?难道就没有更好的解决办法吗?

最佳答案

只要在 View Controller A 消失时不删除监听器,那么当您对推送到堆栈顶部的 View Controller 进行更改时,应该调用监听器。我已经让它发挥作用了。在监听器代码中,一定要检查它不是来自缓存,因为有时它会调用多次。您还可以使用此代码检查文档更改。

snapshot.documentChanges.forEach { diff in
if (diff.type == .added) {
print("New city: \(diff.document.data())")
}
if (diff.type == .modified) {
print("Modified city: \(diff.document.data())")
}
if (diff.type == .removed) {
print("Removed city: \(diff.document.data())")
}
}

关于ios - 如果未显示 View Controller (导航 Controller 的后堆栈),则不会调用 Firestore addSnapshotListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48816200/

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