【发布时间】:2017-06-03 02:27:15
【问题描述】:
我有一个 tableviewcontroller,其中列出了登录用户发送给应用程序中任何他/她选择的用户的所有消息:
登录用户和应用程序的任何其他用户之间的消息使用以下代码附加并显示在 tableviewcell 中:
func loadData()
{
FIRDatabase.database().reference().child("messages").observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in
if let postsDictionary = snapshot .value as? [String: AnyObject] {
for post in postsDictionary {
let messages = post.value as! [String: AnyObject]
for (id, value) in messages {
let info = value as! [String: AnyObject]
let convoId = info["convoId"]
let toId = info["ReceiverId"] as! String!
let fromId = info["senderId"] as! String!
if (toId == self.loggedInUserUid || fromId == self.loggedInUserUid) {
let ref = FIRDatabase.database().reference().child("messages").child(convoId as! String)
ref.observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let message = Message(dictionary: dictionary)
// self.messages.append(message)
if let receiver = message.convoId {
self.messagesDictionary[receiver] = message
self.messages = Array(self.messagesDictionary.values)
print(self.messages)
self.messages.sort(by: { (message1, message2) -> Bool in
return (message1.timestamp?.int32Value)! > (message2.timestamp?.int32Value)!
})
}
//this will crash because of background thread, so lets call this on dispatch_async main thread
DispatchQueue.main.async(execute: {
self.MessageTableView.reloadData()
})
}
}, withCancel: nil)}
}
}}})
}
但是,当我单击一个单元格时,我想转到一个详细的视图控制器,该控制器显示登录用户和单击的单元格的用户之间的消息,所以我使用此代码进行 segue:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let message = messages[indexPath.row]
if message.ReceiverId != self.loggedInUserUid {
var newVariable = message.ReceiverId
if let userpicuid = newVariable {
let ref = FIRDatabase.database().reference().child("users").child(userpicuid)
ref.observeSingleEvent(of: .value, with: { (snapshot)
in
if let dictionary = snapshot.value as? [String: AnyObject]{
for post in dictionary {
let messages = post.value as! [String: AnyObject]
for (id, value) in messages {
let username = messages["username"] as? String
self.userpicuid = userpicuid
self.username = username
}}}})}} else if message.senderId != self.loggedInUserUid {
let newVariable = message.senderId
if let userpicuid = newVariable {
let ref = FIRDatabase.database().reference().child("users").child(userpicuid)
ref.observeSingleEvent(of: .value, with: { (snapshot)
in
if let dictionary = snapshot.value as? [String: AnyObject]{
for post in dictionary {
let messages = post.value as! [String: AnyObject]
for (id, value) in messages {
let username = messages["username"] as? String
self.userpicuid = userpicuid
self.username = username
}}}})}
}
performSegue(withIdentifier: "MessageNow", sender: self)
}
override public func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "MessageNow", let chatVc = segue.destination as? SendMessageViewController else {
return
}
print(self.userpicuid)
chatVc.senderId = self.loggedInUser?.uid
chatVc.receiverData = self.userpicuid
chatVc.senderDisplayName = self.userpicuid
chatVc.username = self.username
}
但是,当我单击单元格时,我收到错误 fatal error: unexpectedly found nil while unwrapping an Optional value 所以我包含了一些打印语句来找出 nil 的位置。结果 userpicuid 和用户名返回 nil。我不确定我做错了什么,特别是因为我的代码正在检查 receiverId 和 senderId,并且其中之一不是登录用户的 uid,那么该 uid 应该是与详细消息相关的 senderId。
错误在于 let receiverId = receiverData!详细消息视图控制器中的行:
var receiverData: String?
override func viewDidLoad() {
super.viewDidLoad()
let receiverId = receiverData!
let receiverIdFive = String(receiverId.characters.prefix(5))
let senderIdFive = String(senderId.characters.prefix(5))
if (senderIdFive > receiverIdFive)
{
self.convoId = senderIdFive + receiverIdFive
}
else
{
self.convoId = receiverIdFive + senderIdFive
}
}
【问题讨论】:
-
你的代码在哪一行被破坏了?
-
@KKRocks 我更新了问题
-
尝试将属性声明为 var receiverData : String?
-
@KKRocks 仍然无法正常工作
标签: ios firebase firebase-realtime-database tableview