英文:
SwiftUI Firestore data doesn't showing on the first load
问题
我连接了我的应用程序到Firestore。一切都正常工作,除了在首次启动应用程序时数据不显示。当我在选项卡之间切换时,数据会显示出来。它表现得像[Place]数组在我的应用程序显示之后缓慢添加一样。如何强制在第一次加载时显示数据?以下是代码:
class FirebaseManager: ObservableObject {
@Published var places = [Place]()
init() {
fetchData()
}
func fetchData(){
Firestore.firestore().clearPersistence()
let db = Firestore.firestore()
db.collection("places").addSnapshotListener { (snap, err) in
if err != nil{
print((err?.localizedDescription)!)
return
}
self.places.removeAll()
for i in (snap?.documentChanges)!{
let name = i.document.data()["name"] as! String
let type = i.document.data()["type"] as! String
let desc = i.document.data()["desc"] as! String
let image = i.document.data()["image"] as! String
let loc = i.document.data()["loc"] as! String
let contact = i.document.data()["contact"] as! String
let fol = i.document.data()["followers"] as! String
DispatchQueue.main.async {
self.places.append(Place(id: fol, name: name, type: type, description: desc, image: image, location: loc, contact: contact))
print(name)
}
}
}
}
}
struct TopView: View {
@ObservedObject var firebaseMan = FirebaseManager()
var body: some View {
VStack(alignment: .leading, spacing: 15) {
Text("Clubs & Places")
.font(.system(size: 25, weight: .regular))
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 15) {
ForEach(0 ..< firebaseMan.places.count) { i in
Image(self.firebaseMan.places[i].image)
// Your image view code here
}
}
}
}
}
}
我尝试使用.onAppear{...
在视图上,但也没有帮助。谢谢。
英文:
i connected my app wirh Firestore. Everything works fine exept for my data does not show first time when i launch the app. When i switch between tabs, the data shows. It's accting like [Place] array is appending slowly after my app shows. How to force to show data on the first load? here is code:
class FirebaseManager: ObservableObject {
@Published var places = [Place]()
init() {
fetchData()
}
func fetchData(){
Firestore.firestore().clearPersistence()
let db = Firestore.firestore()
db.collection("places").addSnapshotListener { (snap, err) in
if err != nil{
print((err?.localizedDescription)!)
return
}
self.places.removeAll()
for i in (snap?.documentChanges)!{
let name = i.document.data()["name"] as! String
let type = i.document.data()["type"] as! String
let desc = i.document.data()["desc"] as! String
let image = i.document.data()["image"] as! String
let loc = i.document.data()["loc"] as! String
let contact = i.document.data()["contact"] as! String
let fol = i.document.data()["followers"] as! String
DispatchQueue.main.async {
self.places.append(Place(id: fol, name: name, type: type, description: desc, image: image, location: loc, contact: contact))
print(name)
}
}
}
}
}
struct topView : View {
@ObservedObject var firebaseMan = FirebaseManager()
var body : some View{
VStack(alignment: .leading, spacing: 15){
Text("Clubs & Places")
.font(.system(size: 25, weight: .regular))
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 15){
ForEach(0 ..< firebaseMan.places.count) {i in
Image(self.firebaseMan.places[i].image)
I tried with .onAppear{... on view, but that doesn't help too.
Thanks.
答案1
得分: 1
已解决!此代码没问题,但在SwiftUI中,ScrollView存在一个问题,即它不会刷新可观察对象!在ScrollView之后,始终检查是否(your @Published var)=!nil!我希望苹果会在下一个更新中尽快解决这个问题。
英文:
Resolved! This code is ok, but in SwiftUI ScrollView has a problem that it's not refreshing observasble object! After ScrollView always check if (your @Published var) =! nil! I hope Apple will resolve this problem soon, in next update.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论