iOS & Swift 공부 - Retrieving Data From Firestore (영)

김영채 (Kevin)·2021년 2월 6일
0

iOS & Swift

목록 보기
69/107
post-thumbnail

Retrieving Data From Firestore


→ Take a look at the Firebase documents. There is a section about "Read Data"

db.collection("messages").getDocuments() { (querySnapshot, err) in
    if let err = err {
        print("Error getting documents: \(err)")
    } else {
        for document in querySnapshot!.documents {
            print("\(document.documentID) => \(document.data())")
        }
    }
}

→ Since the name of our collection is "messages" our code to retrieve data should look something like above.

func loadMessages(){

   db.collection(K.FStore.collectionName).getDocuments { (querySnapshot, err) in
            
    if let err = err{
         print("There was an issue retrieving data from Firestore \(err)")
    }
    else{ 
        if let snapshotDocuments = querySnapshot?.documents{
            for doc in snapshotDocuments{
                 print(doc.data())
             }
         }
       }
     }
}

→ We can use the above code to test if we are correctly retrieving the data

→ Our Firestore has a collection named "messages", and inside a single document, we have values we need to retrieve

→ If we run the code above, we will be able to get the value from Firestore

→ In our case, since we are making a chat app, we would have to retrieve the messages in Firestore when viewDidLoad( ) runs.

override func viewDidLoad() {
        super.viewDidLoad()
        
        loadMessages()
    }

func loadMessages(){
        
        messages = [] // Emptying our messages array
        
        db.collection(K.FStore.collectionName).getDocuments { (querySnapshot, err) in
            
            if let err = err{
                print("There was an issue retrieving data from Firestore \(err)")
            }
            else{
                
                if let snapshotDocuments = querySnapshot?.documents{
                    
                    for doc in snapshotDocuments{
                        let data = doc.data()
                        if let messageSender = data[K.FStore.senderField] as? String, let messageBody = data[K.FStore.bodyField] as? String{
                            
                            let newMessage = Message(sender: messageSender, body: messageBody)
                            self.messages.append(newMessage)
                             
                            DispatchQueue.main.async {      // We are in a closure, meaning the method 수행 is happening in the
                                                            // background. But when we need to update the tableView, we have to
                                                            // do this in the main thread
                                self.tableView.reloadData()
                            }
                        }
                    }
                    

                }
            }
        }
    }
profile
맛있는 iOS 프로그래밍

0개의 댓글