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
How to Change Wkwebview or Uiwebview Default Font
Using Nsuserdefaults with Xcode 8 and iOS 10
How to Add Images for Different Screen Size from Assets.Xcassets in Xcode 8
How to Rotate Orientation Programmatically in Swift
Trying to Set Only Time in Uidatepicker in Swift 2.0
Cannot Assign a Value of Type "String" to Type "Uilabel" in Swift
Swift: Can't Get Nsdate Dateformatter Right
Realmswift: Convert Results to Swift Array
iOS UI Testing on an Isolated View
What Are the Device-Width CSS Viewport Sizes of the Iphone6 and iPhone 6 Plus
Uitableview Checkmark Only One Row at a Time
Launch Apple Mail App from Within My Own App
Can't Assign Multiple Buttons to Uinavigationitem When Using Storyboard with iOS 5
Change the Width of Master in Uisplitviewcontroller
Making a Button Persistent Across All View Controllers
How to Programmatically Enable Guided Access (Kiosk Mode) on an Iphone