如何获取用户信息?

时间:2019-08-01 20:39:01

标签: ios swift firebase firebase-realtime-database

我想在uid表数据中检索数据User,但是对于一个特定用户,我有2个用户,似乎它抓住了2个用户uid,但我想用speficy来抓住不只是他们两个。 预先谢谢你

    let specificDatabase = Database.database().reference()

    specificDatabase.queryOrdered(byChild: "User/FirstName").queryEqual(toValue: "The user first name")

    specificDatabase.observeSingleEvent(of: .value) { (snapShot: DataSnapshot) in

        for child in snapShot.children {
            print(snapShot.key)
        }
    }

 Firebase Data Structure
"User" : {

"ez8sTAsqXTWfnuzizUXU69VS4qM2" : {
  "FirstName" : "other",
  "LastName" : "Martin",
  "uid" : "ez8sTAsqXTWfnuzizUXU69VS4qM2"
},
} 

 "Data" : {
"ez8sTAsqXTWfnuzizUXU69VS4qM2" : {
  "-Ll7jUYg6BxRAhWPLskg" : {
    "Name" : "other Martin",
    "Data1" : "data"
  },
  "-Ll7jW_elQIPTLESwDYD" : {
    "Name" : "other Martin",
    "Data1" : "data "
  }
},
 }

1 个答案:

答案 0 :(得分:1)

您要告诉Firebase通过其User/FirstName属性对根的每个子节点进行排序,然后对其进行过滤。由于根节点的子节点在该路径上没有属性,因此查询不会返回任何结果。

相反,您想通过其/User属性来对FirstName的每个子节点进行排序/过滤,您可以使用:

let specificDatabase = Database.database().reference(withPath: "User")

specificDatabase.queryOrdered(byChild: "FirstName").queryEqual(toValue: "other")