How to remove from Firebase database

Hi, I followed this guide: https://www.youtube.com/watch?v=XIQsQ2injLo

This explains how to save and retrieve from the database, but not delete. I am wondering how to delete the node database that belongs to the deleted cell. Thanks

+4
source share
1 answer

Change Code updated for Swift 3 and Swift 4 .

I always use remove with a completion handler:

static let ref = Database.database().reference()

static func remove(child: String) {

    let ref = self.ref.child(child)

    ref.removeValue { error, _ in

        print(error)
    }
}

So, for example, if I want to remove the following value:

enter image description here

I call my function: remove(child: "mixcloudLinks")

If I want to go deeper and delete, for example, β€œadded”:

enter image description here

I need to change the function a bit.

static func remove(parentA: String, parentB: String, child: String) {

    self.ref.child("mixcloudLinks").child(parentA).child(parentB).child(child)

    ref.removeValue { error, _ in

        print(error)   
    }
}

Called as:

let parentA = "DDD30E1E-8478-AA4E-FF79-1A2371B70700"
let parentB = "-KSCRJGNPZrTYpjpZIRC"
let child = "added"
remove(parentA: parentA, parentB: parentB, child: child)

/ ""

autoID autoID , .

, , :

func store(item: MyClassObject) {

    var item = item.toJson()

    let ref = self.ref.child("MyParentFolder").childByAutoId()
    item["uuid"] = ref.key // here I'm saving the autoID key into the dictionary to be able to delete this item later
    ref.setValue(item) { error, _ in

        if let error = error {

           print(error)
        }
    }
}

autoID :

enter image description here

.child(MyClassObject.uuid).remove..., .

+21

Source: https://habr.com/ru/post/1655424/


All Articles