【问题标题】:Firebase not returning dataFirebase 不返回数据
【发布时间】:2021-12-13 07:36:53
【问题描述】:

我有以下用于从 Firebase 实时数据库检索数据的快速代码:

    private let db = Database.database().reference(withPath: "food")

    func fetchFoods() {
        db.getData(completion: { error, snapshot in
            guard error == nil else {
                print("ERROR")
                print(error!.localizedDescription)
                return
            }
            print(snapshot.value as? String ?? "Unknown")
            // Food data is stored as a map of UUIDs to Food objects
            guard let foodDicts = snapshot.value as? [String: Any] else {
                print("ERROR: type cast failed")
                return
            }
        })
    }

但是我有一个奇怪的问题,如果我的数据是这样存储的:

{
  "food": [{
      "id": "8466A419-AE6E-4997-BE74-220B59C95F96",
      "cholestrol": 0,
      "macroProfile": {
        "carbProfile": {
          "sugar": 0,
          "fiber": 0
        },
      ...
  }]
}

检索数据没有问题。但是,如果数据是这样存储的:

{
  "food": {
      "8466A419-AE6E-4997-BE74-220B59C95F96": {
        "cholestrol": 0,
        "macroProfile": {
          "carbProfile": {
            "sugar": 0,
            "fiber": 0
          },
        ...
      }
   }
}

我得到:

Unable to get latest value for query FQuerySpec (path: /, params: {
}), client offline with no active listeners and no matching disk cache entries

【问题讨论】:

  • 将您的数据集树显示为图像。

标签: json swift firebase firebase-realtime-database


【解决方案1】:

我通过编辑 database.rules.json 并将读写规则设置为 true 来解决此问题。

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

您可能会问,为什么其他数据格式有效?因为它是从以前缓存的!在我的规则文件被更改的过程中的某个地方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多