如何根据用户是否是朋友在Swift上过滤Firestore用户?

时间:2019-04-15 20:28:35

标签: swift xcode firebase google-cloud-firestore

我正在尝试组装一个基本的约会应用程序,并且需要了解如何获取匹配的用户并将其加载到单独的ChatLobby页面上的帮助。这是我目前的位置:

// (1) when currentUser rejects/likes another user, this is saved into firestore as 0/1, respectively.

fileprivate func saveSwipeToFirestore(didLike: Int) {
    guard let uid = Auth.auth().currentUser?.uid else { return }

    guard let cardUID = topCardView?.cardViewModel.uid else { return }

    let documentData = [cardUID: didLike]

    Firestore.firestore().collection("swipes").document(uid).getDocument { (snapshot, err) in
        if let err = err {
            print("Failed to fetch swipe document: ", err)
            return
        }

        if snapshot?.exists == true {
            Firestore.firestore().collection("swipes").document(uid).updateData(documentData) { (err) in
                if let err = err {
                    print("Failed to save swipe data: ", err)
                    return
                }
                print("Successfully updated swipe...")

                if didLike == 1 {
                    self.checkIfMatchExists(cardUID: cardUID)
                }
            }
        } else {
            Firestore.firestore().collection("swipes").document(uid).setData(documentData) { (err) in
                if let err = err {
                    print("Failed to save swipe data: ", err)
                    return
                }
                print("Successfully saved swipe...")

                if didLike == 1 {
                    self.checkIfMatchExists(cardUID: cardUID)
                }
            }
        }
    }
}

上面的代码加载了currentUser(当前已登录)已刷过的用户的词典,键是用户uid上已刷过的uid,该对显示了currentUser是否已向左刷过(Int == 0)或正确(整数== 1):

[“ 1i8BNLIXBlbtwNSqw3JkX3QDDNl2”:1,“ faImO8S61VNjXNe8CGa2SuDLmXf2”:1,“ jittehkU6bNON9Feowh8xJt9ey62”:1]

// (2) if two users have swiped right on eachother, call a function which loads their profile pic and name into ChatLobbyController (where matches are shown)
fileprivate func checkIfMatchExists(cardUID: String) {

    Firestore.firestore().collection("swipes").document(cardUID).getDocument { (snapshot, err) in
        if let err = err {
            print("Failed to fetch document for card user:", err)
            return
        }

        guard let data = snapshot?.data() else { return }
        print(data)

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

        let hasMatched = data[uid] as? Int == 1
        if hasMatched {
            addMatchToFriends(cardUID: String)
        }
    }
}

// (3) not sure how to place this info into addMatchToFriends
fileprivate func addMatchToFriends(cardUID: String) {
    ??????
}

我对执行(3)背后的逻辑有一个想法,但是不确定如何在代码中正确执行,有人能提出一些建议吗?用户的信息存储在一个名为“用户”的单独集合中,该集合同时包含我所关注的“名称”和“ imageUrl”字段,并且还将我使用的uid存储在“滑动”字典中。也可以随时要求我提供更多信息。谢谢。

0 个答案:

没有答案