gpt4 book ai didi

ios - swift 与Firebase聊天

转载 作者:行者123 更新时间:2023-11-30 11:26:39 26 4
gpt4 key购买 nike

我使用了这个模型:

import Foundation
class Chat {
var message: String?
var senderID: String
var receiverID: String
var timestamp : String

init(messageTextString: String?, senderIDNumber: String, receiverIDNumber: String, timeStampString: String){
message = messageTextString
senderID = senderIDNumber
receiverID = receiverIDNumber
timestamp = timeStampString
}
}


对于ChatViewController:

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return chats.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! ChatCollectionViewCell
let senderIDNumber = Auth.auth().currentUser?.uid
if chats[indexPath.row].senderID == senderIDNumber {
if let chatsText = chats[indexPath.row].message{
let size = CGSize(width: 250, height: 1000)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let estimatedFrame = NSString(string: chatsText).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 18)], context: nil)
cell.messageSend.frame = CGRect(x:8,y:0,width:estimatedFrame.width + 16, height:estimatedFrame.height + 20)
cell.textBubbleView.frame = CGRect(x:0,y:0,width:estimatedFrame.width + 16 + 8, height:estimatedFrame.height + 20)
//showOutgoingMessage(text: chats[indexPath.row].message)
}
cell.messageSend.text = chats[indexPath.row].message
}
else {
/*cell.messageReceived.text = chats[indexPath.row].message */
let chatsText = chats[indexPath.row].message
let size = CGSize(width: 250, height: 1000)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let estimatedFrame = NSString(string: chatsText!).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 18)], context: nil)
cell.messageSend.frame = CGRect(x:view.frame.width - estimatedFrame.width,y:0,width:estimatedFrame.width + 16, height:estimatedFrame.height + 20)
cell.textBubbleView.frame = CGRect(x:view.frame.width - estimatedFrame.width,y:0,width:estimatedFrame.width + 16 + 8, height:estimatedFrame.height + 20)
}
return cell
}

@objc func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
if let chatsText = chats[indexPath.row].message {
let size = CGSize(width: 250, height: 1000)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let estimatedFrame = NSString(string: chatsText).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 18)], context: nil)
return CGSize(width: view.frame.width, height: estimatedFrame.height + 20)
}
return CGSize(width: view.frame.width, height: 200)
}

func loadPosts() {
let ref = Database.database().reference()
let senderIDNumber = Auth.auth().currentUser?.uid
ref.child("chats").queryOrdered(byChild: "senderID").queryEqual(toValue: senderIDNumber).observe(.childAdded) { (snapshot: DataSnapshot) in
if let dict = snapshot.value as? [String: Any] {
let messageText = dict["message"] as! String
let senderIDNumber = dict["senderID"] as! String
let receiverIDNumber = dict["receiverID"] as? String
let timestamp = dict["timestamp"] as? String
let chat = Chat(messageTextString: messageText, senderIDNumber: senderIDNumber, receiverIDNumber: receiverIDNumber!, timeStampString: timestamp!)
//append(post) to array
self.chats.append(chat)
print(self.chats)
self.collectionView.reloadData()
}
}
}

func loadPostsReceivedMessage() {
let ref = Database.database().reference()
ref.child("chats").queryOrdered(byChild: "receiverID").queryEqual(toValue: receiverIDNumber).observe(.childAdded) { (snapshot: DataSnapshot) in
if let dict = snapshot.value as? [String: Any] {
let messageText = dict["message"] as! String
let senderIDNumber = dict["senderID"] as! String
let receiverIDNumber = dict["receiverID"] as? String
let timestamp = dict["timestamp"] as? String
let chat = Chat(messageTextString: messageText, senderIDNumber: senderIDNumber, receiverIDNumber: receiverIDNumber!, timeStampString: timestamp!)
self.chats.append(chat)
print(self.chats)
self.collectionView.reloadData()
}
}
}

}


我希望能够区分呼入和呼入消息,但无法做到这一点。但不知道如何执行。
我需要帮助和建议,很多事情

最佳答案

Firebase无法执行查询!!!(尤其是多重排序)。。需要迁移到firestorm(firebase存储的新Beta版本)或使用angular。

关于ios - swift 与Firebase聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50668482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com