Using Updatechildvalues to Delete from Firebase

Using UpdateChildValues to delete from Firebase

So the issue here is that

ref.updateChildValues(childUpdates)

requires a [String: AnyObject!] parameter to updateChildValues, and AnyObject! cannot be a nil (i.e. you can't use AnyObject? which is an optional that could be nil)

However, you can do this

let childUpdates = [path1 : NSNull(),
path2 : NSNull(),
path3 : NSNull(),
path4 : NSNull()]

Because AnyObject! is now an NSNull() object (not nil), and Firebase knows that NSNull is a nil value.

Edit

You can expand on this to also do multi-location updates. Suppose you have a structure

items
item_0
item_name: "some item 0"
item_1
item_name: "some item 1"

and you want update both item names. Here's the swift code.

func updateMultipleValues() {
let path0 = "items/item_0/item_name"
let path1 = "items/item_1/item_name"

let childUpdates = [ path0: "Hello",
path1: "World"
]

self.ref.updateChildValues(childUpdates) //self.ref points to my firebase
}

and the result is

items
item_0
item_name: "Hello"
item_1
item_name: "World"

Swift: Firebase updateChildValues function Overwriting and Deleting Other Keys at Location

Currently you are updating the objects with objects like this:

let childUpdates = ["/users/\(userID!)": driver,
"/status/\(userID!)/driverInfo/vehicle": vehicle]

You are giving the object (a dictionary) as the value of the childUpdates dictionary. What this does is that replace all the children with this object meaning deleting the values for which you are sending nil for example in your case you have not included info or infoThat .

Now if you want to change only the values you want to give like only change the value of say make and model for vehicle and currentEvent for driver , you have to give the particular path for these values

["/users/\(userID!)/currentEvent": eventID,
"/status/\(userID!)/driverInfo/vehicle/make": make.text, "/status/\(userID!)/driverInfo/vehicle/model": model.text]

I think this will update the values of these locations as you expected.

HTH...

How to remove firebase database


db = Database.database().reference() 
let usersReference = db
usersReference!.removeValue()

Remove an entire child from Firebase using Swift

I understand you don't have access to the key of the node you want do delete. Is that right? Why not? If you use the "observe()" function on a FIRDatabaseQuery object, each returned object should come with a key and a value.

Having a key it is easy to remove a node, as stated in the Firebase official guides.

From the linked guide,

Delete data

The simplest way to delete data is to call removeValue on a reference
to the location of that data.

You can also delete by specifying nil as the value for another write
operation such as setValue or updateChildValues. You can use this
technique with updateChildValues to delete multiple children in a
single API call.

So, you could try:

FirebaseDatabase.Database.database().reference(withPath: "Forum").child(key).removeValue()

or

FirebaseDatabase.Database.database().reference(withPath: "Forum").child(key).setValue(nil)

If you can't get the key in any way, what you said about "iterating" through the children of the node could be done by using a query. Here's some example code, supposing you want all forum posts by Jonahelbaz:

return FirebaseDatabase.Database.database().reference(withPath: "Forum").queryOrdered(byChild: "username").queryEqual(toValue: "Jonahelbaz").observe(.value, with: { (snapshot) in
if let forumPosts = snapshot.value as? [String: [String: AnyObject]] {
for (key, _) in forumPosts {
FirebaseDatabase.Database.database().reference(withPath: "Forum").child(key).removeValue()
}
}
})

Here you create a sorted query using as reference "username" then you ask only for the forum posts where "username" are equal to Johanelbaz. You know the returned snapshot is an Array, so now you iterate through the array and use the keys for deleting the nodes.

This way of deleting isn't very good because you might get several posts with the same username and would delete them all. The ideal case would be to obtain the exact key of the forum post you want to delete.

Deleting all files in a firebase folder?

If you have this in your database:

users: {
user9266622: {
id: 9266622,
name: "SylvainC"
},
user209103: {
id: 209103,
name: "Frank van Puffelen"
}
}

Then you can delete a specific property with something like:

firebase.database().ref("/users/user209103/name").remove();

You can remove an entire branch by calling remove() on the top of that branch. So to remove "my" entire profile:

firebase.database().ref("/users/user209103").remove();

I think this last snippet is what you're looking for: it removes /users/user209103 and everything under it.

The last line of the documentation you quote is about updating/removing multiple branches/properties in distinct locations in one call. For example, say you want to remove the id property from both profiles (since they're redundant). You can do this with:

firebase.database().ref().update({
"/users/user9266622/id": null,
"/users/user209103/id": null
});

This is called a multi-location update, since you're updating two locations by their full path.

Firebase Real-time Database iOS Delete Key

That i see: you have to fill completion block of removeValue method...
And you can also delete by specifying nil as the value for another write operation such as setValue or updateChildValues.



Related Topics



Leave a reply



Submit