对于两个匹配的用户,在Firebase / Swift上创建聊天室的首选方式是什么? [英] What's a preferred way of creating a chatroom on Firebase/Swift for two matched users?

查看:281
本文介绍了对于两个匹配的用户,在Firebase / Swift上创建聊天室的首选方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



是否从Firebase基础参考创建聊天室对象,并将聊天室分配给两个用户,然后插入聊天室的密钥到两个用户?



我只是困惑如何去做,因为我已经写了代码开始上面的想法,但我怎么能确定一旦聊天室被创建,用户将永远拥有那个房间,而不是为他们初始化一个全新的房间?我想我错了......现在我有了代码的方式,聊天室将在消息视图控制器上运行这段代码:

  override func viewDidAppear(animated:Bool){
super.viewDidAppear(animated)
$ b $ currentUserKey = DataService.ds.REF_CURRENT_USER。键

DataService.ds.REF_CURRENT_USER.observeSingleEventOfType(.value的,withBlock:在

{快照如果让matchesInterestedIn = snapshot.value [ matchesInterestedIn] {
如果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]()

如果快照!= nil {
为snapshot.children潜在匹配{
让潜在匹配=潜在匹配。值中的输出格式()
如果潜在匹配[matchesInterestedIn]!=无{
如果让potentialMatchInterests =潜在匹配数据[matchesInterestedIn]? 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 self.admirers中的潜在匹配{
,如果self.currentUserInterests.contains(potentialMatch.key){
self.matches.append(潜在匹配)

let chatRoomInitializer = [ USER1:self.currentUserKey, 用户2:potentialMatch.key]
让chatRoomRef = DataService.ds.REF_CHATROOMS.childByAutoId()
让chatRoomID = chatRoomRef.key

//对于一些奇怪的原因,代码的下两行创建聊天室的对象从基部参考
无尽的金额让currentUserChatRoomRef = DataService.ds.REF_CURRENT_USER.childByAppendingPath(聊天室)。childByAutoId()
currentUserChatRoomRef.setValue(chatRoomID)

让potentialMatchRef = DataService.ds.REF_USERS.childByAppendingPath(potentialMatch.key).childByAppendingPath( 聊天室)。childByAutoId()
potentialMatchRef.setValue(chatRoomRef.key)
$ b $ chatRoomRef.setValue(chatRoomInitializer)




$ b .tableView.reloadData()
})
}


解决方案

常见的方法是根据该房间中的用户创建房间名称。所以如果你的uid是 ron 而我的 puf ,我们结束了一个房间 puf_ron



请注意,我在连接之前订购了uid,因此无论谁在用户列表中排在第一位,它们的顺序都是相同的。



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


I'm working on creating a Tinder clone on Firebase 100%, from authentication up to real-time chat. I've been successful to the point of showing users their mutually interested matches on the Messages View Controller's tableview. Now my problem lies in creating a chatroom for the matched users. What is the most efficient way of going about this?

Do I create chatroom objects from the Firebase base reference, and assign the chatroom both users, and plug in the chatroom's key into both users?

I'm just confused on how to go about that, because I've written the code to start on that idea above, but how can I make sure that once a chatroom is created, the users will always have that room, and not have a brand new room initialized for them? I think I'm going about it the wrong way... The way I have the code now, the chat rooms will be made on the Messages View Controller when I run this block of code:

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()
    })
}

解决方案

A common way is to create a room name based on the users in that room.

So if your uid is ron and mine is puf, we end up in a room puf_ron.

Note that I ordered the uids before concatenating, so that they are in the same order no matter who happened to end up first in the list of users.

This approach doesn't require keeping track of what rooms a user is in and ensures that the same two (or more) users always end up in the same room.

这篇关于对于两个匹配的用户,在Firebase / Swift上创建聊天室的首选方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆