我尝试使用firebase在两个客户端之间创建聊天 首先进入视图我下载最后50条消息: 这里是代码:
current_room_id_url.queryLimitedToLast(50).queryOrderedByChild("date").observeSingleEventOfType(FEventType.Value) { (snapshot:FDataSnapshot!) -> Void in
if let values = snapshot.value as? NSDictionary{
for item in values {
print(item)
if !self.keys.contains(snapshot.key){
self.keys.append(item.key as! String)
if let message = item.value as? NSDictionary{
let date = message["date"] as! NSTimeInterval
let receiveSenderID = message["senderId"] as! String
let displayName = message["senderDisplayName"] as! String
self.createAvatar(receiveSenderID, senderDisplayName: displayName, color: UIColor.jsq_messageBubbleGreenColor())
let jsqMessage = JSQMessage(senderId: receiveSenderID , senderDisplayName: displayName, date: NSDate(timeIntervalSince1970: date), text: message["message"] as! String)
self.messages.append(jsqMessage)
}
}
}
//self.collectionView?.reloadData()
self.messages.sortInPlace({ ($0.date.compare($1.date) == NSComparisonResult.OrderedAscending)})
self.finishReceivingMessageAnimated(true)
}
}
之后我想在RealTime中发送所有消息:我正在使用该功能:
current_room_id_url.queryLimitedToLast(1).observeEventType(.ChildAdded) { (snapshot:FDataSnapshot!) -> Void in
if !self.keys.contains(snapshot.key){
self.keys.append(snapshot.key)
if let message = snapshot.value as? NSDictionary{
let date = message["date"] as! NSTimeInterval
let receiveSenderID = message["senderId"] as! String
let receiveDisplayName = message["senderDisplayName"] as! String
self.createAvatar(receiveSenderID, senderDisplayName: receiveDisplayName, color: UIColor.jsq_messageBubbleGreenColor())
let jsqMessage = JSQMessage(senderId: receiveSenderID, senderDisplayName: receiveDisplayName, date: NSDate(timeIntervalSince1970: date), text: message["message"] as! String)
self.messages.append(jsqMessage)
if receiveSenderID != self.senderId{
JSQSystemSoundPlayer.jsq_playMessageReceivedSound()
}
}
self.finishReceivingMessageAnimated(true)
}
}
当我尝试使用debreakpoint调试时,我看到Im只有在我退出并返回视图时才进入第二个功能,而显然当我在chatRoom时我需要它... 谢谢!