firebase observableObject在应用程序入睡后不更新
我有一个类似于可观察到的类的课程,可以收听Firestore集合。一切都起作用,直到应用入睡(例如30分钟后),并且云功能运行以更新一些数据。然后,直到我再次杀死并打开应用程序,直到我得到最新更新后,实时更新才不再发生。
我的代码正常工作:
class FirebaseRealTime: ObservableObject {
..
@Published var myUsers = [Users]()
..
self.listenToUserCollection()
..
func listenToUserCollection {
db.collection("users").addSnapshotListener { (querySnapshot, error) in
DispatchQueue.global(qos: .background).async {
..
DispatchQueue.main.async {
self.myUsers = tempUsers
//self.usersLoaded = true
}
..
}
}
然后,在场景委托中设置了一个全局var作为环境对象
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
..
var userDetails = FirebaseRealTime()
..
let contentView = ViewExample()
.environmentObject(userDetails)
..
}
,我有一个SwiftUI视图接收实时数据。
struct ViewExample: View {
@EnvironmentObject var userDetails:FirebaseRealTime
@State var users:[Users] = []
var body: some View {
VStack {
ScrollView {
ForEach(users) { user in
RowExample(user: user)
}
}
}
.onReceive(userDetails.$myUsers) { data in
print (data)
}
}
}
正如我说的那样,当应用程序处于活动状态时,我会在Firestore中手动更改数据更新,但是当Google Cloud Func在后端运行时,它不会。
知道发生了什么事吗?有没有办法“强制”接收到的数据更新或其他工作?
I have a class set as ObservableObject to listen to a Firestore collection. Everything works until the app goes asleep (eg. after 30 mins) and a Cloud Function runs to update some data. Then the real time updates no longer happen until I kill and open the app again, only after that I get the most recent updates.
My code is working like this:
class FirebaseRealTime: ObservableObject {
..
@Published var myUsers = [Users]()
..
self.listenToUserCollection()
..
func listenToUserCollection {
db.collection("users").addSnapshotListener { (querySnapshot, error) in
DispatchQueue.global(qos: .background).async {
..
DispatchQueue.main.async {
self.myUsers = tempUsers
//self.usersLoaded = true
}
..
}
}
Then a global var is set in the scene delegate as an environment object
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
..
var userDetails = FirebaseRealTime()
..
let contentView = ViewExample()
.environmentObject(userDetails)
..
}
Last, I have a SwiftUI view receiving the real time data.
struct ViewExample: View {
@EnvironmentObject var userDetails:FirebaseRealTime
@State var users:[Users] = []
var body: some View {
VStack {
ScrollView {
ForEach(users) { user in
RowExample(user: user)
}
}
}
.onReceive(userDetails.$myUsers) { data in
print (data)
}
}
}
As I said when the app is active and I manually change a field in Firestore the data updates, but when the Google Cloud func runs on the backend it does not.
Any idea what's going on? Is there a way to "force" the received data to get updated, or any other work around?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看您遇到的主要问题,似乎不使用
观察到
,而是应该使用state> stateBoct
。由于SwiftUi可以随时构建或破坏视图,因此在一个内部创建一个 @
观察>
是危险的。为了确保视图重新绘制时的一致结果,请使用@stateObject包装器,除非您将 @obsedObject
作为依赖关系。指南是,无论如何创建您的对象的任何视图都必须使用 @
stateBoct
通知Swiftui它是数据的所有者,并且可以维护它。所有其他视图都必须使用 @观察到
向Swiftui表示他们要注意对对象的更改,但不能直接拥有它。@
stateObject
和 @obsedobject
共享许多特征,而SwiftUI则以不同的方式管理其生活周期。要确保当当前视图生成观察到的对象时,请使用状态对象属性包装器。每当您将观察到的对象作为依赖项注入 @@
obsedObject
。Reviewing the main issue you are getting, it seems that instead of using
ObservedObject
you should useStateObject
.Since SwiftUI could at any time construct or destroy a view, it is dangerous to create a @
ObservedObject
inside of one. To guarantee consistent results upon a view redraw, utilize the @StateObject wrapper unless you inject the @ObservedObject
as a dependency.The guideline is that whatever view creates your object first must use @
StateObject
to inform SwiftUI that it is the owner of the data and it is in charge of maintaining it. All other views must use @ObservedObject
to indicate to SwiftUI that they want to keep an eye out for changes to the object but do not directly own it.While @
StateObject
and @ObservedObject
share many traits, SwiftUI manages their life cycles differently. To guarantee consistent outcomes when the current view generates the observed object, use the state object property wrapper. You can use the @ObservedObject
whenever you inject an observed object as a dependency.如果问题仅在应用程序进行背景之后发生,我认为如果您撤销侦听器并在应用程序再次进入前景时添加另一个侦听器会有所帮助,类似的事情是:
收听App状态在视图中的变化并委托到该应用程序。查看模型:
然后在视图模型中重新列入更改:
If the problem happens only after the app goes background, I think it will help if you revoke the listener and add another listener when the app goes to the foreground again, something like this:
Listen to app state changes in the view and delegate to the view model:
Then in the view model re-listen to the changes again: