3
votes

is there a way to perform an atomic delete in Firebase?

the docs say

You can also delete by specifying nil as the value for another write operation such as setValue or updateChildValues...Simultaneous updates made this way are atomic: either all updates succeed or all updates fail.

but I can't get it to work. My code below returns an error

Cannot store object of type _SwiftValue at . Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray

Is there something I'm missing here or are the docs wrong?

var topNodeRef = FIRDatabase.database().reference()
var childUpdates: [String:Any?] = [:]

childUpdates.updateValue(nil, forKey: "/owner")
childUpdates.updateValue(nil, forKey: "/pet")

topNodeRef.updateChildValues(childUpdates)
2

2 Answers

3
votes

in case anyone else runs into the same problem, I managed to fix it by using NSNull() instead of nil

var topNodeRef = FIRDatabase.database().reference()
var childUpdates: [String:Any] = [:]

childUpdates["/owner"] = NSNull()
childUpdates["/pet"] = NSNull()

topNodeRef.updateChildValues(childUpdates)
0
votes

The syntax seems slightly different from what I use in this working snippet:

var childUpdates: [String:Any?] = [:]

childUpdates["-K9i1z2KPLzh9_OrAaQU"] = nil
childUpdates["-K9i2kfteaS7tap93Gf6"] = nil

ref?.updateChildValues(childUpdates)