UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Views not updating while observing Firebase

Forums > SwiftUI

I'm using an @ObservedObject function that observes my Firebase Database. What is the best way to make a view update when a change has been observed? I'm currently using .onAppear(), but that forces me to refresh the view to see the changes. I'm new to Swift, so any help is appreciated.

Thanks, Parker

1      

@Bnerd  

Not an expert, but did you go through this? https://firebase.google.com/docs/firestore/query-data/listen

1      

OK here is what you can do, start a timer , update onreceive of timer every second, once the view is about to disappear . stop the process, remember to stop the timer once you leave the view ie on its disappear modifier , this way you get continuos updates with out need for refresh ... hope it helps

struct ContentView: View {
    @State var currentDate = Date()
    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

    var body: some View {
        Text("\(currentDate)")
            .onReceive(timer) { input in
                currentDate = input
            }
    }
}

2      

@AmitShrivastava Thank you! That worked. Saved me hours of work.

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.