JSQMessages ViewController聊天,无需在Database中创建任何子项

时间:2017-11-17 12:49:42

标签: ios jsqmessagesviewcontroller

我在JSQMessagesViewController中使用了Viewcontroller。聊天将与匿名用户打开。聊天工作正常,但我需要在数据库中进行一些更改。因为数据库是它的firebase。我正在我的数据库中创建一个孩子,当任何消息进入它显示的数据库时,首先它显示名为“Message”的孩子,并在消息下显示所有聊天消息,我想删除这个孩子,直接聊天应该在进入数据库时​​显示,并且应该直接解除所有消息,而不会干扰数据库中的任何子项。我的代码就是聊天,

import UIKit
import Firebase

import FirebaseDatabase

@objc class ChatViewViewController: JSQMessagesViewController, UINavigationControllerDelegate{

 var ref: DatabaseReference!
var Name : String?


 private var messages = [JSQMessage]()
lazy var messageRef = Database.database().reference().child("Message")
 var newMessageRefHandle: DatabaseHandle?
lazy var outgoingBubbleImageView: JSQMessagesBubbleImage = self.setupOutgoingBubble()
lazy var incomingBubbleImageView: JSQMessagesBubbleImage = self.setupIncomingBubble()
 private var updatedMessageRefHandle: DatabaseHandle?
override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "Chat Room"
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(addTapped))

    self.ref = Database.database().reference(fromURL: "Database URL")

    guard let str = UserDefaults.standard.string(forKey: "name") else {
        return
    }
    senderDisplayName = str

    self.senderId = Auth.auth().currentUser?.uid as NSString?! as String!

    collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSize.zero
    collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSize.zero

    observeMessages()
    // Do any additional setup after loading the view.

}

发送按钮代码就是这个,

 override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) {
    let itemRef = messageRef.childByAutoId()
    let messageItem = [
        "senderId": senderId!,
        "senderName": senderDisplayName!,
        "text": text!,
        ]

    itemRef.setValue(messageItem)

    JSQSystemSoundPlayer.jsq_playMessageSentSound()

    collectionView.reloadData()

    finishSendingMessage()


}

用于在UI中显示消息,

 private func observeMessages() {
    //messageRef = channelRef!.child("messages")


    // 1.
    let messageQuery = messageRef.queryLimited(toLast:25)

    // 2. We can use the observe method to listen for new
    // messages being written to the Firebase DB
    newMessageRefHandle = messageQuery.observe(.childAdded, with: { (snapshot) -> Void in
        // 3
        let messageData = snapshot.value as! Dictionary<String, String>

        if let id = messageData["senderId"] as String!, let name = messageData["senderName"] as String!, let text = messageData["text"] as String!, text.characters.count > 0 {
            // 4
            self.addMessage(withId: id, name: name, text: text)

            // 5
            self.finishReceivingMessage()

                }

    })

    updatedMessageRefHandle = messageRef.observe(.childChanged, with: { (snapshot) in
                          })
}

这就是我的数据库外观 enter image description here

1 个答案:

答案 0 :(得分:0)

我不知道我是否完全理解您的问题是什么,但我可以从您收集到的内容中尝试从您的数据库中删除Message图层。您应该可以通过更改文件顶部的这一行来完成此操作

lazy var messageRef = Database.database().reference().child("Message")至 - &gt; lazy var messageRef = Database.database().reference()

这将删除该图层。如果这不是您所描述的问题,请告诉我。