Lobo Lobo - 1 month ago 9
Swift Question

What is the exact way of Firebase (3.2.0) Querying with Swift (2.0)?

I have been trying to query through my firebase database by using their guides but I'm unable to return results appropriately.
If someone can point out what I'm doing wrong or correct me, it will be very helpful.

Here are my code snippets

let ref = DataService.dataService.BASE_REF
let myQuery = (ref.child("testing")).queryOrderedByKey()
let MyQuery1 = (ref.child("testing")).queryOrderedByValue()
let MyQuery2 = (ref.child("testing")).queryEqualToValue("Alphaa")
let MyQuery3 = (ref.child("testing")).queryEqualToValue("Alphaa", childKey: "Alpha")
myQuery.observeSingleEventOfType(.Value, withBlock: {
snapshot in
print("Ordered By Key")
print(snapshot.value)
})

MyQuery1.observeSingleEventOfType(.Value, withBlock: {
snapshot in
print("Ordered by Value")
print(snapshot.value)
})

MyQuery2.observeSingleEventOfType(.Value, withBlock: {
snapshot in
print("Value Equal")
print(snapshot.value)
})

MyQuery3.observeSingleEventOfType(.Value, withBlock: {
snapshot in
print("Ordered by Value & Key")
print(snapshot.value)
})


Here is the result screen
Both order by key and Value don't sort it out.

Optional({
Alpha = Gammaa;
Alright = 24;
Beta = Betaa;
Delta = Deltaa;
Epsilon = Epsilonn;
Eta = Etaa;
Gamma = Alphaa;
Iota = Iotaa;
Lets = 3;
Okay = 1;
Theta = Thetaa;
Zeta = Zetaa;
beep = 4;
boop = 11;
})


Both Value Equal & Ordered by Value & Key give me Optional(null)

JSON table values

"testing" : {
"Alpha" : "Gammaa",
"Alright" : 24,
"Beta" : "Betaa",
"Delta" : "Deltaa",
"Epsilon" : "Epsilonn",
"Eta" : "Etaa",
"Gamma" : "Alphaa",
"Iota" : "Iotaa",
"Lets" : 3,
"Okay" : 1,
"Theta" : "Thetaa",
"Zeta" : "Zetaa",
"beep" : 4,
"boop" : 11
}


I'm trying to return equal values or sort it from the server end instead on the client end.

Answer

I continued testing and changed the way i would get my result by

 MyQuery1.observeSingleEventOfType(.Value, withBlock: {
            snapshot in
            print("************")
            print("Ordered by Value")
            for snap in snapshot.children {
                print(snap);
            }
        })

this gave me the result i wanted for values :D and also for keys

Ordered by Value
Snap (Okay) 1
Snap (Lets) 3
Snap (beep) 4
Snap (boop) 11
Snap (Alright) 24
Snap (Gamma) Alphaa
Snap (Beta) Betaa
Snap (Delta) Deltaa
Snap (Epsilon) Epsilonn
Snap (Eta) Etaa
Snap (Alpha) Gammaa
Snap (Iota) Iotaa
Snap (Theta) Thetaa
Snap (Zeta) Zetaa