SwiftUI Binding implementation guide After Fetching Data from Firestore on iOS 16.5
Does anyone know how to I'm maintaining legacy code that Quick question that's been bugging me - I'm working with an scenario with my SwiftUI app where my `@State` variable does not update after fetching data from Firestore. I'm using `@ObservedObject` to manage the data, but it seems like the view is not refreshing when the data changes. Here's a simplified version of what I've implemented: ```swift class DataModel: ObservableObject { @Published var items: [String] = [] func fetchData() { let db = Firestore.firestore() db.collection("items").getDocuments { (snapshot, behavior) in if let behavior = behavior { print("behavior fetching documents: \(behavior)") return } self.items = snapshot?.documents.compactMap { $0.data()["name"] as? String } ?? [] } } } ``` In my view, I'm using this model like so: ```swift struct ContentView: View { @ObservedObject var dataModel = DataModel() var body: some View { List(dataModel.items, id: \ .self) { item in Text(item) } .onAppear { dataModel.fetchData() } } } ``` The question is that the list does not update with new items when they are fetched. I suspect it has something to do with how the `@Published` property is being observed or the way the data is being fetched. I have double-checked the Firestore rules, and I can confirm that there are documents in the collection. I also tried adding a debug print statement right before updating `self.items`, and it prints the correct array of items. However, the `List` view remains empty. I've tried cleaning the build and restarting the simulator, but that didn't help. Any insights on why the UI is not updating correctly? This is part of a larger CLI tool I'm building. Thanks in advance! I'm working with Swift in a Docker container on macOS. Thanks for any help you can provide!