简体   繁体   English

Swift Firebase-基于UID的项目数组

[英]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 aaccdc2e-1095-4b09-8397-fc0f51f437f5:是客户UID
  • XBQDIaMo: is the order number (each order has unique alphanumeric number). XBQDIaMo:是订单号(每个订单都有唯一的字母数字)。 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. 我想做的是由当前用户查询此结构(相同的UID与上面的第二个项目符号匹配),然后按DueDate字符串对结果进行排序/排序,如图像中的属性所示。

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. QueryByValue将查看Firebase参考中所有键/值对的值。 The uid in your case is a key, not a value. 您的情况下的uid是键,而不是值。 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. 它的值将是快照中其下的对象的字典,并且它将不查询它,因为该字典不等于您的uid。 Instead just append the reference's path with the uid and then call .observeEventType 而是只需将引用的路径附加到uid,然后调用.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?!")
                        }
                    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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