在 Firebase/Swift 上为两个匹配的用户创建聊天室的首选方式是什么?



我正在Firebase上100%创建Tinder克隆,从身份验证到实时聊天。我已经成功地在消息视图控制器的表视图上向用户显示了他们相互感兴趣的匹配项。现在我的问题在于为匹配的用户创建一个聊天室。做这件事最有效的方法是什么?

我是否从Firebase基本引用创建聊天室对象,并为两个用户分配聊天室,并将聊天室的密钥插入两个用户?

我只是对如何做到这一点感到困惑,因为我已经编写了从上面的想法开始的代码,但我如何确保一旦创建了聊天室,用户将始终拥有该聊天室,而不是为他们初始化一个全新的聊天室?我想我做这件事的方式不对。。。按照我现在的代码,当我运行以下代码块时,聊天室将在消息视图控制器上创建:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    currentUserKey = DataService.ds.REF_CURRENT_USER.key
    DataService.ds.REF_CURRENT_USER.observeSingleEventOfType(.Value, withBlock: { snapshot in
        if let matchesInterestedIn = snapshot.value["matchesInterestedIn"] {
            if matchesInterestedIn != nil {
                for (_, value) in matchesInterestedIn as! [String: String]  {
                    self.currentUserInterests.append(value)
                }
            }
        }
    })
    DataService.ds.REF_USERS.observeSingleEventOfType(.Value, withBlock: { snapshot in
        self.admirers = [Match]()
        self.matches = [Match]()
        if snapshot != nil {
            for potentialMatch in snapshot.children {
                let potentialMatchData = potentialMatch.valueInExportFormat()
                if potentialMatchData["matchesInterestedIn"] != nil {
                    if let potentialMatchInterests = potentialMatchData["matchesInterestedIn"] as? Dictionary<String, String> {
                        if potentialMatchInterests.values.contains(self.currentUserKey) {
                            let interestedMatch = Match(snapshot: potentialMatch as! FDataSnapshot)
                            self.admirers.append(interestedMatch)
                        }
                    }
                }
            }
        }
        if self.admirers.count > 0 {
            for potentialMatch in self.admirers {
                if self.currentUserInterests.contains(potentialMatch.key) {
                    self.matches.append(potentialMatch)
                    let chatRoomInitializer = ["user1": self.currentUserKey, "user2": potentialMatch.key]
                    let chatRoomRef = DataService.ds.REF_CHATROOMS.childByAutoId()
                    let chatRoomID = chatRoomRef.key
                    // For some odd reason, the next two lines of code create an endless amount of chatroom objects from the base reference
                    let currentUserChatRoomRef = DataService.ds.REF_CURRENT_USER.childByAppendingPath("chatrooms").childByAutoId()
                    currentUserChatRoomRef.setValue(chatRoomID)
                    let potentialMatchRef = DataService.ds.REF_USERS.childByAppendingPath(potentialMatch.key).childByAppendingPath("chatrooms").childByAutoId()
                    potentialMatchRef.setValue(chatRoomRef.key)
                    chatRoomRef.setValue(chatRoomInitializer)
                }
            }
        }
        self.tableView.reloadData()
    })
}

一种常见的方法是根据房间中的用户创建房间名称。

所以,如果你的uid是ron,而我的是puf,我们最终会在一个房间puf_ron

请注意,我在连接之前对uid进行了排序,这样无论用户列表中的第一个用户是谁,它们的顺序都是相同的。

这种方法不需要跟踪用户所在的房间,并确保相同的两个(或多个)用户最终总是在同一个房间。

最新更新