簡體   English   中英

從Firebase刪除表視圖單元格

[英]delete table view cell from 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)}
                    }

                }}})

    }

如果對Firebase實時數據庫節點進行了更新,則在該節點上進行讀取的任何用戶都將看到相同的值(如果安全規則允許用戶讀取該節點)。 因此,要回答您的問題-您實際上無法對數據庫路徑進行用戶特定的更新。

話雖如此,您可以執行以下操作之一:

  1. 您可以刪除要在客戶端刪除的內容,以使其不再可見。 但是,這不會刪除數據庫中的數據,下次您從同一路徑下載時,您會再次看到它。

  2. 您可以為兩個用戶使用單獨的對話路徑,並在這兩個位置中存儲相同的消息,而不是共同的消息。 用這種方法,您將復制數據。 但是,如果您要進行用戶特定的更改,那么這將不僅僅是為您做的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM