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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.