Lobo Lobo - 1 year ago 87
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")

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

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

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

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

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 Source

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

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

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download