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.