繁体   English   中英

Swift Firebase-基于UID的项目数组

[英]Swift Firebase - Array of items based on UID

我有以下数据结构 在此处输入图片说明

这是这里每个节点含义的简要概述:

  • 订单:这是我跟踪所有客户订单的地方
  • aaccdc2e-1095-4b09-8397-fc0f51f437f5:是客户UID
  • XBQDIaMo:是订单号(每个订单都有唯一的字母数字)。 在每个订单号下方是可以在图像中看到的所有属性。

我想做的是由当前用户查询此结构(相同的UID与上面的第二个项目符号匹配),然后按DueDate字符串对结果进行排序/排序,如图像中的属性所示。

如何才能做到这一点?

这是我现在用来尝试查询的代码,但是它没有打印出任何快照,表明做错了。

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将查看Firebase参考中所有键/值对的值。 您的情况下的uid是键,而不是值。 它的值将是快照中其下的对象的字典,并且它将不查询它,因为该字典不等于您的uid。 而是只需将引用的路径附加到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