【发布时间】:2021-12-31 06:03:47
【问题描述】:
我有一个 iOS 小部件,我想在从 Firestore 检索数据后更新小部件视图。我浏览了整个互联网,包括this question。检索数据后,我无法更新视图。请查看以下代码以获取小部件的视图。数据已检索,但我无法在检索数据后更新 UI 元素。
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
}
}
}
}
检索数据后如何更新TextView?
【问题讨论】:
-
我在您的代码中没有看到
TextView。表明这一点似乎很重要。 -
文本视图在矩形的 .overlay 内。