SwiftUI Firestore数据在第一次加载时不显示

huangapple go评论74阅读模式
英文:

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(&quot;places&quot;).addSnapshotListener { (snap, err) in
            if err != nil{
                print((err?.localizedDescription)!)
                return
            }
            
     self.places.removeAll()
            
            for i in (snap?.documentChanges)!{
                let name = i.document.data()[&quot;name&quot;] as! String
                let type = i.document.data()[&quot;type&quot;] as! String
                let desc = i.document.data()[&quot;desc&quot;] as! String
                let image = i.document.data()[&quot;image&quot;] as! String
                let loc = i.document.data()[&quot;loc&quot;] as! String
                let contact = i.document.data()[&quot;contact&quot;] as! String
                let fol = i.document.data()[&quot;followers&quot;] 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(&quot;Clubs &amp; Places&quot;)
                .font(.system(size: 25, weight: .regular))
            
            
            ScrollView(.horizontal, showsIndicators: false) {
                
                HStack(spacing: 15){
                    
                    ForEach(0 ..&lt; 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.

huangapple
  • 本文由 发表于 2020年1月4日 01:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定