使用 Swift 从带有条件的 Firebase 数据库中获取数据

Posted

技术标签:

【中文标题】使用 Swift 从带有条件的 Firebase 数据库中获取数据【英文标题】:Get data from Firebase database with conditions using Swift 【发布时间】:2017-08-07 17:02:35 【问题描述】:

我有这样的数据库结构:

如您所见,我有两个集合:“线程”和“用户” 我需要接收 ownedIdwVlUM2Un9kNouOIlztKLvxxxPDh1 的所有线程,例如

【问题讨论】:

您需要的是一个查询。他们涵盖在Firebase documentation 和these questions 中的一些可能也有很好的例子。 正如弗兰克指出的那样,查询可能是解决此问题的最简单方法。另一种选择是循环遍历线程对象并将具有正确 id 的线程添加到数组中。 @FrankvanPuffelen 看来你已经回答过了 - ***.com/a/39647861/877223 【参考方案1】:

您好,我在开发一个使用 firebase 作为后端服务的聊天应用程序时遇到了类似的情况。

您可以使用这样的查询,我通过类似的查询做到了

    FireBase.Reference.threads
        .queryOrdered(byChild: "ownerId")
        .queryEqual(toValue: "YourUserId")
        .observe(.value)  (snapshot) in
            if let messages = snapshot.value as? NSDictionary 
                // you will get all those threads where ownedId is what you need to fetch for
            
    

希望对您有所帮助,如果需要更正,请告诉我。

【讨论】:

以上是关于使用 Swift 从带有条件的 Firebase 数据库中获取数据的主要内容,如果未能解决你的问题,请参考以下文章