ios - Firebase get child name by value and remove it -
my json looks this:
users username1: "wwodh96yr3qs4n3gwdvq3olqffb2" username2: "rj6pzttlmsg222ouygwhtwpvhdg1"
i "username1" key (auth.uid)! , if found want delete it, after delete looks this:
users username2: "rj6pzttlmsg222ouygwhtwpvhdg1"
.
self.ref.child("users").queryorderedbyvalue().queryequal(tovalue: user?.uid).observe(.value, with: { snapshot in if snapshot.exists() { print("\(snapshot.value)") snapshot.ref.child(snapshot.value as! string).removevalue() // deleting every thing wrong! } else{ print("not found--") } self.ref.removeallobservers() }) }
this code deleting in users. want delete specific user.
first of all, approach if want query 1 value @ 1 moment, doing, observesingleevent (is best approach?). solve problem, forgot add "users" path. below code more safe use, since force unwrap (which not recommend case):
let dataref = self.ref.child("users") dataref.keepsynced(true) if let usersuid = auth.auth().currentuser?.uid{ dataref.child("users").queryorderedbyvalue().queryequal(tovalue: usersuid).observesingleevent(.value, with: { snapshot in print(snapshot) let values = snapshot.value as? nsdictionary (key, value) in values{ print(value) // print uid? print(key) // or one? self.ref.child("users/\(key)").removevaluewithcompletionblock({ (err, ref) in if err != nil { print(err) } else { print(ref) print("removed") } }) } }) }
try add this, right after print(snapshot)
Comments
Post a Comment