访问Firebase中的用户uid

时间:2016-09-09 15:05:09

标签: json swift firebase firebase-realtime-database nosql

我有这个JSON结构:

     {
"Devices" : {
"1EJ8QmEQBJfBez7PMADbftCjVff1" : {
  "Device1" : {
    "Category" : "Others",
    "Description" : "",
    "DeviceName" : "",
    "ImageUrl" : ""
  },
  "Device2" : {
    "Category" : "Chairs",
    "Description" : "",
    "DeviceName" : "",
    "ImageUrl" : ""
  },
  "Device3" : {
    "Category" : "Others",
    "Description" : "",
    "DeviceName" : "",
    "ImageUrl" : ""
  }
},
"97PUAcUC5UYxLpBOLnC4yQjxiEf2" : {
  "Device1" : {
    "Category" : "Others",
    "Description" : "",
    "DeviceName" : "",
    "ImageUrl" : ""
  },
  "Device2" : {
    "Category" : "Books",
    "Description" : "",
    "DeviceName" : "",
    "ImageUrl" : ""
  },
  "Device3" : {
    "Category" : "Chairs",
    "Description" : "",
    "DeviceName" : "",
    "ImageUrl" : ""
    }
   }
  },
 "UserProfile" : {
"1EJ8QmEQBJfBez7PMADbftCjVff1" : {
  "city" : "",
  "email" : "",
  "name" : "",
  "phone" : ""
},
"97PUAcUC5UYxLpBOLnC4yQjxiEf2" : {
  "city" : "",
  "email" : "",
  "name" : "",
  "phone" : ""
   },
 }
}

我想访问每个设备中的Category节点? 那可能吗? 因为我想检索具有相同类别的所有节点的数据!

let ref = FIRDatabase.database().reference().child("Devices")

    ref.queryOrderedByChild("Category")

我试图访问设备!然后直接到该类别! 看来这不是正确的方法!

不要告诉我需要更改JSON的结构!

有没有办法访问用户ID?

1 个答案:

答案 0 :(得分:0)

在NoSQL数据库中,您经常需要为应用所需的用例建模数据。使用您当前的数据结构,您可以有效地加载用户的设备。

为了有效地允许加载类别的设备,您还必须存储该信息。这是一种方法:

"Categories": {
  "Others": {
    "1EJ8QmEQBJfBez7PMADbftCjVff1_Device1": "1EJ8QmEQBJfBez7PMADbftCjVff1/Device1",
    "1EJ8QmEQBJfBez7PMADbftCjVff1_Device3": "1EJ8QmEQBJfBez7PMADbftCjVff1/Device3",
    "97PUAcUC5UYxLpBOLnC4yQjxiEf2_Device1": "97PUAcUC5UYxLpBOLnC4yQjxiEf2/Device1"
  },
  "Chairs": {
    "1EJ8QmEQBJfBez7PMADbftCjVff1_Device2": "1EJ8QmEQBJfBez7PMADbftCjVff1/Device2",
    "97PUAcUC5UYxLpBOLnC4yQjxiEf2_Device3": "97PUAcUC5UYxLpBOLnC4yQjxiEf2/Device3"
  },
  "Books": {
    "97PUAcUC5UYxLpBOLnC4yQjxiEf2_Device2": "97PUAcUC5UYxLpBOLnC4yQjxiEf2/Device2"
  }

}

因此,上述结构使用<uid>_<devicekey>作为键,然后在/devices下将该项目的路径作为其值。

我建议您在NoSQL data modeling上阅读这篇精彩的文章。