Firebase Database Read childByAutoId - ios

Im currently learning ropes in Firebase for iOS, so bare with my novice skills.
Below is a screenshot of my database:
gZ7dn2LMkiah is the autoId for a user
LDBTRFVS8dtz and LDBTe4oXs are autoIds for childId for this user
How can I read the two child nodes inside the node gZ7dn2LMkiah? Cause from my code below, it can only be possible if I have only one child underneath this node, not two
ref = Database.database().reference()
let userId: String = (Auth.auth().currentUser?.uid)!
databaseHandle = ref?.child("childId").child(userId).observe(.childAdded, with: { (snapshot) in
I tried adding childByAutoId after child(userId) but it didn't return any results.
Any help would be appreciated. Thanks in advance.

Database.database().reference(withPath:
"childId").child(userId).observe(.childAdded)
{ (snapshot:DataSnapshot) in
// This will print all sub node
print(snapshot)
}

First of all your db seems incorrect. In first node nick and relation are inside the autoGeneratedKey and in second node nick and relation are outside the key as both are visible while the node is collapse. So these values should be inside autoGeneratedKey. Please change your db structure in correct way. See below screenshot:
This will be your childs table containing all childs for all parents and you can query for a particular parent to get his childs. See below code snippet:
ref.child("childs").queryOrdered(byChild: "parentId").queryEqual(toValue: "123").observeSingleEvent(of: DataEventType.value) { (snapshot) in
if snapshot.exists() {
print("exists")
for child in snapshot.children {
let data = child as! DataSnapshot
print(data.key)
print(data.value)
}
}
else {
print("doesn't exist")
}
}
Output:
-LDBus9Xas3oTccwPN4r
Optional({
nick = Dave;
parentId = 123;
relation = Son;
})
-LDBus9_Uz_qe69e9CXK
Optional({
nick = Susan;
parentId = 123;
relation = Daughter;
})
Where parentId is let userId: String = (Auth.auth().currentUser?.uid)!.
Note 1: I tried adding childByAutoId after child(userId) but it didn't return any results.
Yes it will not work because childByAutoId generate a new key which will never match with existing db keys so you will get nothing.
Note 2: When to use .childAdded
.childAdded event is to listen new entry for the node reference not to fetch the data. To fetch the data for once we should use .observeSingleEvent event.
Note 3:Cause from my code below, it can only be possible if I have only one child underneath this node, not two
No its not possible. This is just because of second node's nick and relation are outside of the key.

Related

Swift Firebase - getting multiple uid's then adding those uid's to a child

I've been thinking about this but I thought I'd post a question to get some more thinking power behind this or to see if this is even possible. I am grabbing multiple uid's and then want to take these uid's and append them to a child in my database and then add further data to them. Since they are uid's I can't access them separately which would be a easy firebase "update values" call, so how could I take this list of uid's and then add them to a child so they are their own separate children and then add values to them? I am just thinking about how I would set this firebase call to say "add each one of these uid's as its own child".
How I am getting the uid's
func getEmployees() {
let employees = Database.database().reference().child("Businesses").child(self.otherUser?["uid"] as! String).child("registered_employees").observe(.childAdded, with: { (snapshot) in
if snapshot.exists() {
let employess = snapshot.childSnapshot(forPath: "uid")
print(employess)
} else {
print("didnt call right values")
}
})
}
sample of data I would add to uid child
let userMessageRef = Database.database().reference().child("user-messages").child(fromID).child(toID)
let messageId = childRef.key
userMessageRef.updateChildValues([messageId!:1])
The code right above ^^^^ I would want the uid's to be in "toID" and then adding the "messageId" to those uid's
I don't know how I could even do each uid separately in the call because of the inability to extract each one and then set the data.
I think I understand so let me try an answer with an example. How we obtain the uid's we want to write is not outlined in the question so let try this:
Suppose we have a users node that stores our users and if they like pizza
users
uid_0 //the Firebase generated uid
name: "Richie"
likes_pizza: true
uid_1
name: "Marion"
likes_pizza: false
uid_2
name: "Fonzi"
likes_pizza: true
uid_3
name: "Howard"
likes_pizza: false
what we want to do it to get the users that like pizza, craft a new node and store each of the uid's as a parent and then a child of their name.
let usersRef = self.ref.child("users")
let pizzaQueryRef = usersRef.queryOrdered(byChild: "likes_pizza").queryEqual(toValue: true)
pizzaQueryRef.observeSingleEvent(of: .value, with: { snapshot in
guard let allUsers = snapshot.children.allObjects as? [DataSnapshot] else {return}
for user in allUsers {
let key = user.key
let name = user.childSnapshot(forPath: "name").value as! String
let pizzaRef = self.ref.child("pizza_lovers")
let aPizzaLoverRefUid = pizzaRef.child(key).child("their_name")
aPizzaLoverRefUid.setValue(name)
}
})
so this code queries for all users that like pizza (which enables us to access their uid's), and then (per the question) append them to a child in the database and then add further data to them
and then want to take these uid's and append them to a child in my
database and then add further data to them
the result is
pizza_lovers
uid_0
their_name: "Richie"
uid_2
their_name: "Fonzi"
Let me know if I misunderstood the question and I will update.

select Firebase data with queryOrdered(byChild: "name") queryEqual

i want to select a specific name from my firebase-table (look at the picture). The result to print is always empty. The selected name should be "manni". What is wrong in my code? Thx a lot.
#IBAction func BTSelect(_ sender: Any) {
DBref = Database.database().reference()
let query = DBref?.queryOrdered(byChild: "name").queryEqual(toValue: "manni")
query?.observe(.value, with: { (snapshot) in
for childSnapshot in snapshot.children {
print("childSnapshot:-> \(childSnapshot)")
}
})
}
]1
You'd use queryOrdered(byChild: "name") if each child node has a property name. But in your case, you're querying a node named name and you want to filter on the value of the child nodes. To do that, use queryOrderedByValue:
let query = DBref?.child("name").queryOrderedByValue).queryEqual(toValue: "manni")
This first orders the child nodes of name on their value, and then only returns the ones matching manni.
You need to change this:
let query = DBref?.queryOrdered(byChild: "name").queryEqual(toValue: "manni")
to this:
let query = DBref?.queryOrdered(byChild: "-L5o1LyZRP3J_D-OiguB").queryEqual(toValue: "manni")
since the child: name is not equal to manni. A child inside name is equal to manni.
P.S.: i might have copied this wrong -L5o1LyZRP3J_D-OiguB
The child name is a parent node(not equal to anything), and all those nodes with randomids are the children of the child name.
If you want more than one value to be retrieved, it needs to have the same key (key:value), you can then do the below:
You can change your database like this:
myapp2go-app
randomid
name:manni
randomid
name:manni
randomid
name:ulli
randomid
name:Test
randomid
name:alf
randomid
name:ulf
then in your query, keep it like this:
let query = DBref?.queryOrdered(byChild: "name").queryEqual(toValue: "manni")

Firebase Access a children if I don't know two childs who have unknown ids

How can I access the data of the children memberId or name and photoURL of the child "members"?
You can see the structure of my database in images.
I tried to use queryOrdered and queryEqual but I just can use it one time
I tried like that because I know the room.key who is the "key" on the database.
let refParticipants = refDatabase.child("markers").queryOrdered(byChild: "key").queryEqual(toValue: room.key)
refParticipants.observe(.childAdded, with: { snapshot in
...
}
I use Swift 3.1
I update my answer with that screenshot:
I think you are asking how to access the child nodes of
/markers/oHQ.../members/9oBKY...
Let's simply the structure for this answer
markers
marker_0
members
member_0
name: "J"
member)1
name: "K"
and then the code that will access each member within the members node and print their name
let markersRef = self.ref.child("markers")
let marker0Ref = markersRef.child("marker_0")
let membersRef = marker0Ref.child("members")
membersRef.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let snap = child as! DataSnapshot
let dict = snap.value as! [String: Any]
let name = dict["name"] as! String
print(name)
}
})
and the output will be
J
K
Since you know the parent node (oHQa...), which contains the child node 'members', it doesn't really matter as to what each members key is since you are iterating over them.
However, if you are wanting to query for certain members or other member data, you may want to consider flattening the database a bit like this
markers
oHQa...
//marker data
marker_members
member_0
name: "J"
member_of: "oHQa..."
member_1
name: "K"
member_of: "oHQa..."
With this structure you can query for all the members of any marker or query for all members whose name is "J" etc.
As a side note, in the structure in the question you have the member_id as both the key as well as a child which is unnecessary. If it's the key then you can always directly access that node without a query.

iOS - Firebase Filter query

I got score node in Firebase:
And under score node my data structure is as:
{
"jjTO29ziQ2SjrH5PFzY7ZMpSgjq1-Kak6FCyGtdy_kcEPd4K" = {
jjTO29ziQ2SjrH5PFzY7ZMpSgjq1 = "-Kak6FCyGtdy_kcEPd4K";
score = 5;
};
"jjTO29ziQ2SjrH5PFzY7ZMpSgjq1-KbE_pgfUsukOm4uW0bx" = {
jjTO29ziQ2SjrH5PFzY7ZMpSgjq1 = "-KbE_pgfUsukOm4uW0bx";
score = 4;
};
Question:
Can I filter the data by score ?
I tried doing :
FIRDatabase.database().reference().child("scores").queryOrdered(byChild: "score").observeSingleEvent(of: .value, with: { (snapshot) in
debugPrint(snapshot.value ?? "nil")
})
But can't get the result ?
When you execute a query against Firebase, you get a FIRDataSnapshot that contains three pieces of information for each result:
its key
its value
its position relative to other nodes
When you're calling snapshot.value the snapshot is converted into a Dictionary. Unfortunately that dictionary can only hold key-value pairs, so you lose the information on the position of the nodes. So while the data in the snapshot is actually sorted correctly, you're throwing that information away.
The solution is to use the FIRDataSnapshots built-in children property to loop over the child nodes in the correct order. The Firebase documentation on querying has this sample of how to do that:
_commentsRef.observeEventType(.Value, withBlock: { snapshot in
for child in snapshot.children {
...
}
})

How to update value in Firebase with childByAutoId?

When I create objects in Firebase, I use childByAutoId. How can I update these specific objects later? I'm having trouble obtaining the value of the key Firebase automatically updates. Snapshot.key just returns "users". Here's my JSON structure:
{
"users" : {
"-KQaU9lVcUYzIo52LgmN" : {
"device" : "e456f740-023e-440a"
"name: "Test"
}
},
How can I get the -KQaU9lVcUYzIo52LgmN key? I want to update the device child. Here's what I have so far. It currently creates a completely separate snapshot with a single child.
self.rootRef.child("users").queryOrdered(byChild: "name").queryEqual(toValue: self.currentUser).observeSingleEvent(of: .value, with: { (snapshot) in
let key = self.rootRef.child("users").childByAutoId().key
let childValues = ["device": device]
self.rootRef.child("users").child(key).updateChildValues(childValues)
Edit: device is a string set further up in the code. Not defined in this scope (to make it easier to read for this question).
When you get Snapshot.key, it returns "users" because that is the overall key for your snapshot. Everything inside of "users" in your snapshot is considered the value.
You need to iterate over the child layers to dig down to "device".
Try this:
rootRef.child("users").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
for child in result {
var userKey = child.key as! String
if(userKey == userKeyYouWantToUpdateDeviceFor){
rootRef.child("users").child(userKey).child("device").setValue(device)
}
}
}
})
This code will do the following:
Gets snapshot of your reference (the key for that would be
'users').
Gets all the children (your user keys) and assigns them as another
snapshot to 'result'.
Checks each key one at a time until it finds the key you want (for
example, if you look for user with the key "-KQaU9lVcUYzIo52LgmN",
it will find it on the first iteration in your example code you
posted).
Once it finds that key, it sets the value for the device inside that
key with the line
rootRef.child("users").child(userKey).child("device").setValue(device).
Of course, you will need to store all your user keys when you make them. You can maybe use SharedPreferences on the device for this, but if it gets cleared for any reason then that data will just be sitting there. You could also store it on internal storage for your app, but SharedPreferences is what I would use.
Hope this helps!
snapshot has a property key which is
The key of the location that generated this FIRDataSnapshot.
And as you can see you are getting one (snapshot) by calling observeSingleEvent(of: .value, with: { (snapshot)...
so instead of let key = self.rootRef.child("users").childByAutoId().key
try to call let key = snapshot.key
childByAutoId().key always generates new unique key based on timestamp, that's why you are creating new child, not updating the one you want
Hope that works
I adapted Ryan's answer to my own issue (kinda similar) and figured out a way to update your device ID directly without needed to know/store the AutoID key generated by Firebase :
reference = Database.database().reference().child("users")
reference.observeSingleEvent(of: .value, with: { (snapshot) in
if let result = snapshot.children.allObjects as? [DataSnapshot] {
for child in result {
if child.childSnapshot(forPath: "device").value as? String == self.yourDeviceIDVariable {
print("### Device exists in Firebase at key = \(child.key)")
let childKey = child.key
self.reference.child(childKey).child("device").setValue(yourNewDeviceID)
}
}
}
})

Resources