1. Save swipe data to firestore
  2. Update CardViewModel

Save swipe data to firestore

there are two ways to upload data to firestore, updateData & setData But updateData needs existing data, setData always clean datas except data we’re gonna set.

So all we need to do is checking data first

getDocument, snapshot?.exists

Using getDocument, snapshot?.exists to check whether there are any datas

        guard let uid = Auth.auth().currentUser?.uid else { return }

        Firestore.firestore().collection("swipes").document(uid).getDocument { (snapshot, err) in
            if let err = err {
                print("Failed to fetch data from firestore:", err)
                return
            }
            
            if snapshot?.exists == true {
                Firestore.firestore().collection("swipes").document(uid).updateData(documentData, completion: { (err) in
                    if let err = err {
                        print("Failed to update swipe data:", err)
                        return
                    }
                    
                    print("Successfulyy updated swipe data...")
                })
            } else {
                Firestore.firestore().collection("swipes").document(uid).setData(documentData, completion: { (err) in
                    if let err = err {
                        print("Failed to set swipe data:", err)
                        return
                    }
                    
                    print("Successfulyy saved swipe data...")
                })
            }
        }

in firestore data form is always [String: Any]

Update CardViewModel

We need to get each cardView’s user’s uid. there are no uid data in CardViewModel, we have to update CardViewModel

  • Add uid property in CardViewModel

  • User Model already has a uid property, we’ll fix toCardViewModel() function.

Now we can make dataDictionary form for firestore!

        guard let cardUID = topCardView?.cardViewModel.uid else { return }
        let documentData: [String: Any] = [cardUID: 1]

refactoring for swipe

    fileprivate func saveSwipeToFirestore(didLike: Int) {
        guard let cardUID = topCardView?.cardViewModel.uid else { return }
        let documentData: [String: Any] = [cardUID: didLike]

태그: ,

카테고리:

업데이트:

댓글남기기