Reputation: 187
I have a query that checks if a phoneNumber exists in the child "visitors". If the phoneNumber exists, it adds the key of the child to the variable visitorID
If the visitorID != ""
I get the code to do what I want, but when visitorID == ""
I cannot get the code to do anything. Here's my best effort:
ref?.child("visitors").queryOrdered(byChild: "phoneNumber").queryEqual(toValue: phoneNumber).observeSingleEvent(of: .value, with: { (snapShot) in
if let snapDict = snapShot.value as? [String:AnyObject]{
var visitorID = String()
for each in snapDict{
let key = each.key as! String
visitorID = key
}
if visitorID != "" {
//Do something
} else {
//Do something else
}
}
}, withCancel: {(Err) in
print(Err.localizedDescription)
})
Upvotes: 0
Views: 33
Reputation: 35648
If you want to perform a query and determine if there are any results, snapshot.exists is what you want
ref?.child("visitors").queryOrdered(byChild: "phoneNumber").queryEqual(toValue: phoneNumber).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
print("found at least one")
//do something with snapshot
} else {
print("non found, returning")
return
}
}, withCancel: {(Err) in
print(Err.localizedDescription)
})
or
ref?.child("visitors").queryOrdered(byChild: "phoneNumber").queryEqual(toValue: phoneNumber).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() == false {
print("none found, returning")
return
}
//do something with snapshot
Upvotes: 1