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?