Daniel Zafman
Daniel Zafman

Reputation: 35

Why does my function not execute while using a DispatchQueue?

Can someone explain to me why the new view enters before getMsg() finishes executing? I have tried many different iterations and cannot seem to figure out why it is not working. Is the issue with the onAppear()? Or is it with the way I am using the DispatchQueue?

struct ChatView : View {
    @State var msgs = [Msg]()
    @EnvironmentObject var msg : msgDatas
    let group = DispatchGroup()
       var body : some View{
           
           ZStack{
               Color("bg").edgesIgnoringSafeArea(.top)
               
               VStack(spacing: 0){
                   
                   chatTopview()
                   GeometryReader{_ in
                       
                    chatList(msgs: self.msg.msgs).onAppear(){
                        self.group.enter()
                        DispatchQueue.main.async {
                            
                            self.getMsgs()
                            self.group.leave()
                        }
                    }
                    }
                   }
                   
                   chatBottomView()
                   
           }
           .navigationBarTitle("")
               .navigationBarHidden(true)

    }
    
    func getMsgs() {
        let db = Firestore.firestore()
        let uid = UserDefaults.standard.value(forKey: "UserName") as! String
       
        
        db.collection("Private Messages").document(uid).collection("cZQM1Io2azboEUjt42nT6k6TEDF3").order(by: "date", descending: false).addSnapshotListener { (snap, err) in
            if err != nil {
                print(err!.localizedDescription)
            }
            /*
            if snap!.isEmpty {
                
            }
            */
            for i in snap!.documentChanges{
               
                if i.type == .added{
                    let id = i.document.documentID
                    let msg = i.document.get("msg") as! String
                    let user = i.document.get("user") as! String
                    self.msgs.append(Msg(id: id, msg: msg, user: user))
                  
                }
                
               
                     
                
            }
            
    }
    }
   }

Upvotes: 1

Views: 324

Answers (1)

Asperi
Asperi

Reputation: 257779

You need instead something like the following

VStack(spacing: 0){

    chatTopview()
    GeometryReader{_ in

        if self.msg.msgs.isEmpty {
           Text("Loading...")        // << some placeholder
        } else {
            chatList(msgs: self.msg.msgs)
        }
    }
}
.onAppear(){                       // << at this level !!
    if self.msg.msgs.isEmpty {
        self.getMsgs()
    }
}

and in callback, which is asynchronous and can be sent on background queue, it is better to do the following

var newMsgs = [Msg]()
for i in snap!.documentChanges{

    if i.type == .added{
        let id = i.document.documentID
        let msg = i.document.get("msg") as! String
        let user = i.document.get("user") as! String
        newMsgs.append(Msg(id: id, msg: msg, user: user))
    }
}
DispatchQueue.main.async {
    self.msgs = newMsgs
}

Upvotes: 1

Related Questions