I have been searching for an answer and could not find it.
I am using Swift 3.
I have a path, let's say firebase/products, and I want to see if a specific product has a key "MainImage". So naturally I'd do something like
Database.Child("Products").Child("ProductID").HasChild("MainImage")
But there's no method like "HasChild", or "Exists" or anything similar I could find out. Does anyone have the solution for this ?
There is no method that can check for that, you are going to listen to that child and if it has a nil value, then it does not exist.
var ref = FIRDatabase.database().reference()
let ProductID = 64646477343
let requestListenRefo = ref.child("Products/\(ProductID)/MainImage")
requestListenRefo.observe(FIRDataEventType.value, with: { (snapshot) in
let value = snapshot.value as? String
if(value == nil)
{
// doesn't exist
}
})
There is a way to check existence of the snapshot using snapshot.exists()
Swift
dbRef = FIRDatabase.database().reference()
dbRef.child("Users/user1").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists(){
print("user1 exists")
}else{
print("user1 exists doesn't exists")
}
})
Related
I'm new to setting up firebase's realtime database, and I'm trying to access the content from a message to display in the app.
Here is how my database is structured:
I want to access "content" for each of the messages with the fewest value for "numberofresponses."
let ref: DatabaseReference! = Database.database().reference(withPath: "messagepool")
ref.queryOrdered(byChild: "numberofresponses").observeSingleEvent(of: .value, with: { snapshot in
if !snapshot.exists() {
print("no snapshot exists")
return }
print(snapshot)
The above code correctly prints the "messagepool," but I want the specific content value from each of the branches. I seem to be missing something. What is the correct way to do this? Thanks
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
So in your closure/completion handler you will need to loop over snapshot.children to get at the individual result snapshot(s):
ref.queryOrdered(byChild: "numberofresponses").observeSingleEvent(of: .value, with: { snapshot in
if !snapshot.exists() {
print("no snapshot exists")
return
}
for childSnapshot in querySnapShot.children.allObjects as! [DataSnapshot] {
print(childSnapshot.key)
print(childSnapshot.value)
guard let value = childSnapshot.value as? [String: Any] else { return }
do {
guard let content = value["content"] as? String,
...
}
}
}
Also see:
Firebase queryOrderedByChild() method not giving sorted data
Retrieving Data using Firebase Swift
Getting nil in parsing Firebase values swift using Codable and CodableFirebase
I'm trying to fetch the first key from my firebase database but for some reason nothing is being printed out. How can I get the first key from my firebase database using .childAdded
let userMessagesRef = Database.database().reference().child("user-message").child(uid).child(userId)
userMessagesRef.observe(.childAdded, with: { (snapshot) in
guard let first = snapshot.children.allObjects.first as? DataSnapshot else { return }
print(first)
This in incredibly easy if you literally are asking how to only ever get the first child of a node. Here's how to only get the first child of a /users node
func getFirstChild() {
let usersRef = self.ref.child("users")
usersRef.observeSingleEvent(of: .childAdded, with: { snapshot in
print(snapshot)
})
}
or
print(snapshot.key)
if you just want the key.
Or, if you want to use a query to do the same thing
func getFirstChildAgain() {
let usersRef = self.ref.child("users")
let query = usersRef.queryOrderedByKey().queryLimited(toFirst: 1)
query.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot)
})
}
The child_added event is typically used when retrieving a list of items from the database. Unlike value which returns the entire contents of the location, child_added is triggered once for each existing child and then again every time a new child is added to the specified path. The event callback is passed a snapshot containing the new child's data. For ordering purposes, it is also passed a second argument containing the key of the previous child.
From: Read and Write Data on iOS
Per your requirements, this is possible in .value and childAdded.
var child_array = [String:String]
let userMessagesRef = Database.database().reference().child("user-message").child(uid).child(userId)
userMessagesRef.observe(.childAdded, with: { (snapshot) in
let value = snapshot.value as? String ?? "Empty String"
let key = snapshot.key
child_array[key] = value;
}) { (error) in
print(error.localizedDescription)
}
then:
if let first = child_array.first?.key {
print(first) // First Key
}
Big NOTE: child_added randomly collects this data, you should never use it to sort your data
im developing a delivery app. So I have productos and popular products in firebase this way:
Products
PopularProducts(ID of the product as key and true as value)
How I can query only the products who are popular using those childs?
You need to get array of productosPopulares and then get each object.
let referenceToProductosPopulares = FIRDatabase.database().reference(withPath: "productosPopulares")
referenceToProductosPopulares.observeSingleEvent(of: .value, with: { snapshot in
if let value = snapshot.value as? NSDictionary {
let productosPopIds = value.allKeys as! [String]
// get every object
for id in productosPopIds {
let refToProd = FIRDatabase.database().reference(withPath: "productos").child(id)
refToProd.observeSingleEvent(of: .value, with: { snapshot in
// your value
})
}
} else {
print("productosPopulares")
}
})
Not perfect code. Should make more functions. But this is idea.
Hope it helps
You can query ordered by the esPopular child and only return the children with it equal to true.
let ref = Database.database().reference(withPath: "productos")
let query = ref.queryOrdered(byChild: "esPopular").queryEqual(toValue: true)
query.observe(.childAdded, with: { (snapshot) in
print(snapshot)
}) { (error) in
print(error)
}
I have two issues with the snippet below.
I have get duplication in my TableView.
When I search for something that returns a result, and I tried searching for another thing that returns a result, the existing result will be the one showing not the new result, for example:
a. I search for "ola" and my TableView returns a list of -> olabode, olatunde, olaide.
b. Then I searched for "bisi", if found matching query for "bisi" I still get the query result for "ola".
func searchView(_ searchView: AZSearchViewController, didTextChangeTo text: String, textLength: Int) {
self.resultArray.removeAll()
searchView.reloadData()
guard text != "" else {
return
}
AppFirRef.userRef.queryOrdered(byChild: "username")
.queryStarting(atValue: text)
.queryEnding(atValue: text + "\\uf8ff")
.observe(.value, with: { (snapshot) in
if (snapshot.value is NSNull) {
print("not found")
} else {
print("found")
self.resultArray.removeAll()
searchView.reloadData()
print("\(snapshot.value)")
for case let snap as FIRDataSnapshot in snapshot.children {
guard let value = snap.value as? [String : Any] else { continue }
//print("\(snap.key)")
print(value)
let user = LContact(value: value, searchUserId: snap.key)
self.set.add(user)
if let username = user.username{
self.resultArray.append(username.lowercased())
searchView.reloadData()
}
}
}
})
}
It might be too late, but change
.observe(.value, with: { (snapshot) in
...
)}
to
.observeSingleEvent(of: .value, with: { (snapshot) in
...
)}
see my answer to this question for the explanation; it applies to you as well because you are also appending to the array every time the data at your reference path changes (such as when someone on another device changes data in the userRef reference path.
I'm running the code below to see if a user that opens the app is already logged in, and then to check if they've set up a profile. I'm having trouble checking for the null value value returned from the profile check
override func viewDidLoad() {
super.viewDidLoad()
//Check to see if user is already logged in
//by checking Firebase to see if authData is not nil
if ref.authData != nil {
//If user is already logged in, get their uid.
//Then check Firebase to see if the uid already has a profile set up
let uid = ref.authData.uid
ref.queryOrderedByChild("uid").queryEqualToValue(uid).observeSingleEventOfType(.Value, withBlock: { snapshot in
let profile = snapshot.value
print(profile)
})
In the last line when I print(profile), I either get the profile information, or
<null>
how do i check for this value?
if profile == nil
does not work
If I do
let profile = snapshot.value as? String
first, then it always returns nil even if there is a snapshot.value
Take advantage of the exists() method to determine if the snapshot contains a value. To use your example:
let uid = ref.authData.uid
ref.queryOrderedByChild("uid").queryEqualToValue(uid)
.observeSingleEventOfType(.Value, withBlock: { snapshot in
guard snapshot.exists() else{
print("User doesn't exist")
return
}
print("User \(snapshot.value) exists")
})
.
Here's another handy example, Swift4
let path = "userInfo/" + id + "/followers/" + rowId
let ref = Database.database().reference(withPath: path)
ref.observe(.value) { (snapshot) in
let following: Bool = snapshot.exists()
icon = yesWeAreFollowing ? "tick" : "cross"
}
You may want to explore another option: since you know the uid of the user and therefore the path to that user, there's no reason to query. Queries add unnecessary overhead in situations where you know the path.
For example
users
uid_0
name: "some name"
address: "some address"
You are much better off observing the node in question by value, which will return null if it doesn't exist
ref = "your-app/users/uid_0"
ref.observeEventType(.Value, withBlock: { snapshot in
if snapshot.value is NSNull {
print("This path was null!")
} else {
print("This path exists")
}
})
in the event you are storing it some other way; perhaps
random_node_id
uid: their_uid
name: "some name"
then a query would be in order, like this
ref.queryOrderedByChild("uid").queryEqualToValue(their_uid)
.observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
print("you found it!")
}
});