更新Firebase已删除的表格单元格

时间:2017-07-10 15:16:35

标签: ios swift firebase firebase-realtime-database firebase-authentication

我在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

        itemRef.updateChildValues(messageItem) { (error, convoRef) in
        if error != nil{
            print(error)
            return
        }

        let userMessagesRef = FIRDatabase.database().reference().child("user-messages").child(self.senderId!)

        let messageId = itemRef.key
        userMessagesRef.updateChildValues([messageId: 1])

        let userMessagesRefs = FIRDatabase.database().reference().child("user-messages").child(self.senderDisplayName!)

        let messagedId = itemRef.key
        userMessagesRefs.updateChildValues([messagedId: 1])


        }

    }

然后我创建了删除消息单元格的选项。

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    guard let uid = FIRAuth.auth()?.currentUser?.uid else {
        return
    }

    let ref = FIRDatabase.database().reference().child("user-messages").child(uid)
    ref.observe(.childAdded, with: { (snapshot) in

        self.messageId = snapshot.key




        FIRDatabase.database().reference().child("user-messages").child(uid).child( self.messageId!).removeValue(completionBlock: { (error, ref) in
            if error != nil {
                print("error \(error)")
            }else{

            }


        })})

    self.messagesDictionary.removeValue(forKey: uid)
    self.handleReloadTable()



}

但是当表重新加载时,它会崩溃

 func loadData()
    {
        guard let uid = FIRAuth.auth()?.currentUser?.uid else {
            return
        }

        let ref = FIRDatabase.database().reference().child("user-messages").child(uid)
        ref.observe(.childAdded, with: { (snapshot) in

            self.messageId = snapshot.key
        })


        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).child(self.messageId!)
        ref.observeSingleEvent(of: .value, 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)}
                    }

                }}})

    }

并说messageId是一个可选值。

0 个答案:

没有答案