Reputation: 83
My code makes calls to my firebase database, but the order in which it receives the data is incorrect in terms of the function call. It calls the data from ref3 then ref2 then ref4 and I would like for it to retrieve the data in order of ref2, ref3, ref4 of course. No matter what it will always do it in this order.
var ref2: DatabaseReference?
var ref3: DatabaseReference?
var ref4: DatabaseReference?
ref2 = Database.database().reference().child("User data").
ref3 = Database.database().reference().child("User Info").child("Name")
ref4 = Database.database().reference().child("User Info").child("Address")
ref2?.observe(DataEventType.value, with:{(DataSnapshot) in
if DataSnapshot.childrenCount > 0{
for data in DataSnapshot.children.allObjects as![DataSnapshot]{
let proObj = data.value as? [String: AnyObject]
let p: String = proObj?["Username"] as! String
let n: String = proObj?["User login"] as! String
}
}
})
ref3?.observe(DataEventType.value, with:{(DataSnapshot) in
if DataSnapshot.childrenCount > 0{
for data in DataSnapshot.children.allObjects as![DataSnapshot]{
let proObj = data.value as? [String: AnyObject]
let p: String = proObj?["User first name"] as! String
let n: String = proObj?["User last name"] as! String
}
}
})
ref4?.observe(DataEventType.value, with:{(DataSnapshot) in
if DataSnapshot.childrenCount > 0{
for data in DataSnapshot.children.allObjects as![DataSnapshot]{
let proObj = data.value as? [String: AnyObject]
let p: String = proObj?["User email"] as! String
}
}
})
Upvotes: 1
Views: 152
Reputation: 265
When querying data from your firebase database, you are performing an asynchronous call. To put things in simple terms, your code is executed on a different thread and, subsequently, performs parallel operations. This is exactly what is happening in your case.
You are observing data from three different references, and even though you have defined their sequence programmatically, nothing guarantees that the code within the completion handler blocks of your observers will run in that exact same sequence.
If you want to run them sequentially, then you have to nest your observers so that the next database query is executed only after the previous one has finished.
The below should hypothetically work
ref2?.observe(DataEventType.value, with: { (DataSnapshot) in
if DataSnapshot.childrenCount > 0 {
for data in DataSnapshot.children.allObjects as! [DataSnapshot] {
let proObj = data.value as? [String: AnyObject]
let p: String = proObj?["Username"] as! String
let n: String = proObj?["User login"] as! String
}
}
ref3?.observe(DataEventType.value, with: { (DataSnapshot) in
if DataSnapshot.childrenCount > 0 {
for data in DataSnapshot.children.allObjects as! [DataSnapshot] {
let proObj = data.value as? [String: AnyObject]
let p: String = proObj?["User first name"] as! String
let n: String = proObj?["User last name"] as! String
}
}
ref4?.observe(DataEventType.value, with: { (DataSnapshot) in
if DataSnapshot.childrenCount > 0 {
for data in DataSnapshot.children.allObjects as![DataSnapshot] {
let proObj = data.value as? [String: AnyObject]
let p: String = proObj?["User email"] as! String
}
}
}) // ref4 observer
}) // ref3 observer
}) // ref2 observer
Upvotes: 2