我有一個聊天應用程序。用戶可以選擇應用中的其他用戶與之聊天。一旦用戶將消息發送給其他用戶,他們附加的消息就會顯示在一個表格視圖控制器中,當您單擊表格視圖單元格時,您將被關聯到一個詳細的控制器中。從Firebase刪除表格視圖單元格
override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) {
let convoRef = FIRDatabase.database().reference().child("messages").child(convoId!)
let itemRef = rootRef.child("messages").child(convoId!).childByAutoId() // 1
let messageItem = [ // 2
"senderId": senderId!,
"ReceiverId": senderDisplayName!,
"text": text!,
"timestamp": NSDate().timeIntervalSince1970,
"convoId": convoId!
] as [String : Any]
itemRef.setValue(messageItem) // 3
JSQSystemSoundPlayer.jsq_playMessageSentSound() // 4
finishSendingMessage() // 5
isTyping = false
}
所以只有兩個用戶聊天才能查看消息。但我希望一個用戶能夠刪除其應用中的表格視圖單元格(消息),但是當他們這樣做時,它會在Firebase中刪除,這意味着它也會在其他用戶的應用中刪除,但我希望它只在刪除郵件的用戶。下面是消息是如何附加到被顯示在的tableview控制器
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)
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)}
}
}}})
}
選擇編號1是這裏最好的選擇。 OP應該存儲唯一的消息代碼。該唯一的消息代碼應存儲在僅由用戶自己可讀取的用戶文件和數據庫下。如果唯一的消息(數據庫和地圖)匹配 - >刪除消息(客戶端的課程:))。 –
@ J.Doe我很抱歉,您是不是要選擇2號? –