I have an iOS widget in which I would like to update the Widget View after I retrieve data from Firestore. I have looked all over the internet, including at this question. I am unable to update the view after the data is retrieved. Please look at the following code for the widget's view. The data is retrieved, but I am unable to update the UI element after I retrieve the data as I would like.
struct Runner_WidgetEntryView: View {
@State var text = ""
@State var textLoaded = false
var entry: Provider.Entry
var body: some View {
ZStack {
Image("back").resizable().scaledToFit()
Rectangle().foregroundColor(.black).cornerRadius(10).padding([.leading, .trailing], 10).padding([.top, .bottom], 15).overlay(Text(textLoaded ? text : "Loading..."))
}.onAppear {
let dispatchGroup = DispatchGroup()
FirebaseApp.configure()
let db = Firestore.firestore()
dispatchGroup.enter()
db.collection("collection").getDocuments { (snapshot, error) in
if error != nil {
text = "There was an error retrieving the text."
dispatchGroup.leave()
}
if let snapshot = snapshot {
for document in snapshot.documents {
if document.documentID == "Document ID" {
text = document.data()["qoute_name"] as! String
dispatchGroup.leave()
}
}
}
else {
text = "There was an error retrieving the text."
dispatchGroup.leave()
}
}
dispatchGroup.notify(queue: .main) {
textLoaded = true
}
}
}
}
How do I update the TextView
after I retrieve the data?