【问题标题】:Some data is not being read from Firebase database Swift 3某些数据未从 Firebase 数据库 Swift 3 中读取
【发布时间】:2017-09-18 21:21:00
【问题描述】:

我在 firebase 中有一个数据库,看起来像这样
我正在尝试获取给定密钥的 url。但是,我只能获取其中一些键的 url,其余的返回 nil?我看不到为什么有些键会返回我正在寻找的值而有些则不会的模式?这是我正在使用的代码

static func getProfilePictureURL(imageID key: String, completionHandler: @escaping (_ return: String?, _ error: String?) -> Void){
        databaseReference.child("userSelfies").child(userIDGlobal).observeSingleEvent(of: .value, with: { (snapshot) in
            // Get user value

        let value = snapshot.value as? [String: Any]
        print(value!["150575773394828"])

        if let url = value?[key] {
            print("url",url)
            //completionHandler(url, nil)
        }

    }) { (error) in
        print(error.localizedDescription)
        completionHandler(nil,  error.localizedDescription)

    }
}

我已经尝试了所有方法,是的,页面已重新加载,因此数据肯定存在。

【问题讨论】:

    标签: swift firebase firebase-realtime-database


    【解决方案1】:

    所以看起来函数getProfilePictureURL 只需要获取imageID key 的图像,是吗?如果您只查询您要查找的键会发生什么?

    static func getProfilePictureURL(imageID key: String, completionHandler: @escaping (_ return: String?, _ error: String?) -> Void){
            databaseReference.child("userSelfies/\(userIDGlobal)/\(key)).observeSingleEvent(of: .value, with: { (snapshot) in
                // Get user value
    
            let url = snapshot.value as? String
            print("url",url)
            //completionHandler(url, nil)
    
        }) { (error) in
            print(error.localizedDescription)
            completionHandler(nil,  error.localizedDescription)
    
        }
    }
    

    【讨论】:

    • 似乎可以进行一些修改,不知道为什么
    • 我不能用as?字符串,但我必须改用字符串(描述:)
    • 酷!随意编辑答案以显示它对您的作用。
    【解决方案2】:

    好的,所以问题是通往 child 的路径太“深”了。 Jen 的建议几乎是正确的,但路径的尽头(关键)不应该在那里。因为snapshot.value作为NSDictionary返回,带有键值对,我应该做的是将snapshot.value转换为? [String: AnyObject],然后展开并使用带有下标 [key] 的值来获得我想要的值。经过 8 小时的搜索和试验,我终于找到了 swift firebase 数据库...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 2018-02-17
      • 2018-03-26
      相关资源
      最近更新 更多