从Firebase Swift加载聊天 [英] loading chats from firebase Swift

查看:76
本文介绍了从Firebase Swift加载聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用聊天Messenger,并且能够检索我发送给其他用户的所有消息,但无法检索他们发送的任何消息.我用来加载邮件的代码是

I am doing a chat messenger currently and I am able to retrieve all the messages I sent to another user but not able to retrieve whatever they sent. The codes i used to load my messages is

func loadMsg() {
    let toId = user!.id!
    let fromId = Auth.auth().currentUser!.uid
    let ref = Database.database().reference().child("privateMessages").child(fromId).child(toId)
    ref.observe(.value) { (snapshot) in

        if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {

            self.messages.removeAll()

            for data in snapshot {

                let newMsg = Message(dictionary: data.value as! [String: AnyObject])

                self.messages.append(newMsg)

            }
        }
        DispatchQueue.main.async {self.tableView.reloadData()}
    }
}

对于我的Firebase数据库,它看起来像

as for my firebase database, it looks like

和的json文件

{
  "privateMessages" : {
    "0YfqnPIOYFYKb8cYZMHnSYti62i2" : {
      "StHPXFvTlVf5QDvWiuCi4JF8Hyr2" : {
    "-LB0AZRcWF0Ub5ZECzTf" : {
      "fromId" : "0YfqnPIOYFYKb8cYZMHnSYti62i2",
      "textMessages" : "Hi!first msg!",
      "timestamp" : 1524733200,
      "toId" : "StHPXFvTlVf5QDvWiuCi4JF8Hyr2"
    }
  },
  "kj7vcszbSvPBTVaC32Xg18aYZPi1" : {
    "-LB0EpR86dZSYl5p0k-E" : {
      "fromId" : "0YfqnPIOYFYKb8cYZMHnSYti62i2",
      "textMessages" : "Different guy,message 2",
      "timestamp" : 1524734318,
      "toId" : "kj7vcszbSvPBTVaC32Xg18aYZPi1"
    }
  }
},
"2wYq9dCKF4aZ26nOY41ApPOdGrJ2" : {
  "0YfqnPIOYFYKb8cYZMHnSYti62i2" : {
    "-LBV5jlvtxbZZJFQOwes" : {
      "fromId" : "2wYq9dCKF4aZ26nOY41ApPOdGrJ2",
      "textMessages" : "hi",
      "timestamp" : 1525252029,
      "toId" : "0YfqnPIOYFYKb8cYZMHnSYti62i2"
    }
  },
  "StHPXFvTlVf5QDvWiuCi4JF8Hyr2" : {
    "-LBVCQ5GaEugMNFMT2c-" : {
      "fromId" : "2wYq9dCKF4aZ26nOY41ApPOdGrJ2",
      "textMessages" : "hi",
      "timestamp" : 1525253780,
      "toId" : "StHPXFvTlVf5QDvWiuCi4JF8Hyr2"
    }
  }
},
"StHPXFvTlVf5QDvWiuCi4JF8Hyr2" : {
  "0YfqnPIOYFYKb8cYZMHnSYti62i2" : {
    "-LBVGEFG76z6tXtq43k5" : {
      "fromId" : "StHPXFvTlVf5QDvWiuCi4JF8Hyr2",
      "textMessages" : "Hi",
      "timestamp" : 1525254780,
      "toId" : "0YfqnPIOYFYKb8cYZMHnSYti62i2"
    }
  }
}
  },
  "users" : {
"0YfqnPIOYFYKb8cYZMHnSYti62i2" : {
  "email" : "test@yahoo.com",
  "id" : "0YfqnPIOYFYKb8cYZMHnSYti62i2",
  "name" : "tester",
  "profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/groupchatnappointment.appspot.com/o/profile_images%2FE509EFCB-E41D-4E6C-922B-01B146FD1FDC.png?alt=media&token=a7acb904-474b-4898-b99a-1e819ec96afc"
},
"StHPXFvTlVf5QDvWiuCi4JF8Hyr2" : {
  "email" : "test2@yahoo.com",
  "id" : "StHPXFvTlVf5QDvWiuCi4JF8Hyr2",
  "name" : "tester2",
  "profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/groupchatnappointment.appspot.com/o/profile_images%2F2A5009D7-51C4-4D95-88DF-DADB38C76E7B.png?alt=media&token=ae599135-8ff7-4c64-9667-b9a5cec3dcf8"
},
"kj7vcszbSvPBTVaC32Xg18aYZPi1" : {
  "email" : "tester3@yahoo.com",
  "id" : "kj7vcszbSvPBTVaC32Xg18aYZPi1",
  "name" : "tester3",
  "profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/groupchatnappointment.appspot.com/o/profile_images%2FD60F2433-14E2-4EE1-AA74-8171CBA1D3AD.png?alt=media&token=728f6171-e48d-4bae-9b84-007937ed3493"
    }
  }
}

我的tableviewCells具有

and my tableviewCells has a config function of

func configCell(message: Message) {

    self.message = message

    if message.fromId == currentUser {

        sentView.isHidden = false

        sentMsgLabel.text = message.textMessages

        receivedMsgLabel.text = ""

        receivedMsgLabel.isHidden = true

    } else {

        sentView.isHidden = true

        sentMsgLabel.text = ""

        receivedMsgLabel.text = message.textMessages

        receivedMsgLabel.isHidden = false
    }
}

我应该如何尝试在聊天记录中同时显示收件人发送给我的内容以及我发送给他们的内容?

how should i attempt as to be able to show both what my recipient sent to me and what I have sent to them in my chat logs?

推荐答案

实际上,您的节点在这里.child(fromId).child(toId)是错误的,因此您只能获取一条边消息.

Actually your node is wrong here .child(fromId).child(toId) this way you can fetch only one side messages.

let chatRoomId = (fromId < toId) ? fromId + "_" + toId : toId + "_" + formId 
/// It will look like "0YfqnPIOYFYKb8cYZMHnSYti62i2_kj7vcszbSvPBTVaC32Xg18aYZPi1"

,然后将所有消息存储在此处,并在需要提取时以相同的方式制作chatRoomId.这样,您可以轻松获取这两个用户的所有聊天.我也回答了paging.因此,请查看 Firabase分页并按块加载消息.它将带来更好的用户体验.

And then store all the messages here and when need to fetch make the chatRoomId in same way. This way you can easily fetch all the chat b/w these two users. I have also answered for paging. So have a look at Firabase Paging and load messages in chunks. It will make the better user experience.

这篇关于从Firebase Swift加载聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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