如何从Firebase数据库中删除特定的子值

时间:2019-09-10 07:31:21

标签: ios swift firebase firebase-realtime-database

This is my firebase model and I want to delete "specific message node" from my model

///有一个来自firebase的通知,它正在表视图中更新,但是有一个特定的通知我不想在表视图中显示,我只想在viewDidLoad中处理该通知,所以我可以删除

`objRef.child("notification_user").child(MyCurrentUSERID).observe(.value, with: { snapshot in
            if snapshot.childrenCount > 0{
                let allLocationData = snapshot.children.allObjects as! [DataSnapshot]
                var mainArr :[AnyObject] = []
                for item in  allLocationData{

                    let obj = item.value! as! NSDictionary
                    obj.setValue(item.key, forKey: "autoId")
                    mainArr.append(obj as AnyObject)
                }
                let data = Mapper<NotificationModel>().mapArray(JSONObject: mainArr)
                completion(true, data, nil) }`

//这是来自Firebase的快照,我想删除包含消息“组已单击”的快照

{
    action = 3;
    autoId = "-LoOhMMELiTD1Qpx7nNH";
    body = Office;
    isChecked = 0;
    message = "Congratulations! You've been added successfully in Office";
    newMessage = "Congratulations! You've been added successfully in ##";
    receiverID =     (
        LcyvJLDTT1g82CWqXs6YC0XwoEf1
    );
    roomId = 1505182734;
    senderId = 8mvL2JG4alevQfK01fO6OrWs6a33;
    thumbnail = "";
    timestamp = 1568094385768;
    title = Office;
}, {
    action = 106;
    autoId = "-LoPA3Q4Quji42y7yNBp";
    body = "wakeup iOS device";
    isChecked = 0;
    message = "group clicked";
    newMessage = "group clicked";
    receiverID =     (
        LcyvJLDTT1g82CWqXs6YC0XwoEf1
    );
    roomId = 1505182734;
    senderId = 8mvL2JG4alevQfK01fO6OrWs6a33;
    thumbnail = "";
    timestamp = 1568102172213;
    title = "wakeup iOS device";
}

1 个答案:

答案 0 :(得分:1)

  

删除数据

     

删除数据的最简单方法是在引用上调用removeValue   到该数据的位置。

     

您还可以通过指定nil作为另一次写入的值来删除   setValue或updateChildValues之类的操作。你可以用这个   updateChildValues技术可删除一个   单个API调用。

objRef.child("notification_user").observe(.childAdded, with: { (snapshot) -> Void in
  guard let data = snapshot.value as? NSDictionary else {
   print("snapshot empty")
  }
  // Access the message
  if data["message"] != nil && data["message"] == "group clicked" {
     objRef.child("notification_user").child(MyCurrentUSERID).child(snapshot.key).removeValue()
  }

})