简体   繁体   中英

Swift Firebase - Array of items based on UID

I have the following data structure 在此处输入图片说明

This is a brief overview of what each node here means:

  • orders: This is where I keep track of all orders for customers
  • aaccdc2e-1095-4b09-8397-fc0f51f437f5: is the customer UID
  • XBQDIaMo: is the order number (each order has unique alphanumeric number). Under each order number are all the attributes that can be seen in image.

What I want to do is query this structure by the current user (same UID matching the second bullet above) and then sort/order the results by dueDate string as can be seen in the attributes in image.

How can this be done?

Here is the code I am using now to attempt querying but it prints out no snapshot which indicates am doing it wrong..

let reference = Firebase(url:"https://something.firebaseio.com/orders/")
        reference.queryEqualToValue(reference.authData.uid).observeEventType(.Value, withBlock: { snapshot in
            if (snapshot.exists()) {
                print(snapshot.value)
            }else{
                print("No Snapshot?!")
            }
        })

QueryByValue will look at the values of all key/value pairs in your Firebase reference. The uid in your case is a key, not a value. Its value would be the dictionary of objects under it in your snapshot and it won't query it because this dictionary is not equal to your uid. Instead just append the reference's path with the uid and then call .observeEventType

 let reference = Firebase(url:"https://something.firebaseio.com/orders/")

 //This will make your url point to the current users uid within the orders endpoint  

 reference.childByAppendingPath("\(reference.authUser.uid)/") 

 reference.observeEventType(.Value, withBlock: { snapshot in
                        if (snapshot.exists()) {
                            print(snapshot.value)
                            //You should have a dictionary here now. Do a sort by valueForKey

                        }else{
                            print("No Snapshot?!")
                        }
                    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM