【问题标题】:Swift 3: FirebaseDB returning nil when child is presentSwift 3:当孩子存在时,FirebaseDB 返回 nil
【发布时间】:2017-08-20 03:20:30
【问题描述】:

我已将 Firebase 链接到我的应用。当我尝试从数据库中读取数据时,快照中存在数据。但是当一个孩子被读取时,它会返回 nil。

代码如下:

    func checkForDuplicateScan(qrCode: String) {

    DataService.ds.REF_SAMPLES.observeSingleEvent(of: .value, with: { (snapshot) in

        if let dict = snapshot.value as? [String:Any] {
            print(dict)
            print(qrCode)
            print(dict["\(qrCode)"])
            if let sampleDict = dict[qrCode] as? [String:Any] {
                print(sampleDict)
                if let isScanned = sampleDict["scanned"] as? Bool {
                    if isScanned == true {
                        print("Already Scanned")
                        let alert = UIAlertController(title: "Already Redeemed", message: "This offer has already been redeemed by you. Stay tuned.", preferredStyle: UIAlertControllerStyle.alert)
                        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (alert) in
                            self.tabBarController?.selectedIndex = 0
                        }))
                        self.present(alert, animated: true, completion: nil)
                    } else {
                        print("New Scan")
                        self.updateQRCode(qrCode: qrCode)
                    }
                } else {
                    print("Error: can't read/find 'scanned' ")
                }
            }else {
                print("Error: Invalid Code Scanned")
                let alert = UIAlertController(title: "Invalid QR Code Scanned", message: "The code that you've scanned is Invalid.", preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (alert) in
                    self.tabBarController?.selectedIndex = 0
                }))
                self.present(alert, animated: true, completion: nil)
            }
        } else {
            print("Error: can't get dictionary from snapshot value")
        }
    })
}

这是控制台日志:

日志:

  1. 字典来自打印字典。
    打印(字典)

  2. “test13”来自
    打印(二维码)

  3. “零”来自
    print(dict["\(qrCode)"])

这段代码昨天还在工作,但今天失败了。

帮帮我!!

编辑:这是我要读取的数据。

这也是 JSON 文件JSON FILE

更新:看来我在这里发现了问题。

当我运行这段代码时,什么都没有打印出来。

if let newDict = dict[qrCode] as? NSDictionary {
     print(newDict)
}

但是,当我这样做时,字典是可以访问的。

if let newDict = dict["test10"] as? NSDictionary {
     print(newDict)
}

注意 qrCode 是一个字符串,其值为 "test10"

怪怪的!!仍然无法弄清楚其背后的原因以及如何纠正。

【问题讨论】:

  • 我不知道日志中的哪一行来自什么代码。
  • @FrankvanPuffelen 更新
  • 您在问题中包含了 JSON 树的图片。请将其替换为实际的 JSON 作为文本,您可以通过单击 your Firebase Database console 中的导出 JSON 链接轻松获得。将 JSON 作为文本使其可搜索,让我们可以轻松地使用它来测试您的实际数据并在我们的答案中使用它,一般来说这只是一件好事。

标签: firebase swift3 firebase-realtime-database


【解决方案1】:

只是需要检查一下,您的问题似乎与数据类型有关,而不是与 Firebase 数据有关。因此,您将收到“错误:扫描的代码无效”。 我总是选择类似的东西:

if let value = snapshot.value as? NSDictionary {
    let username = value?["username"] as? String ?? ""
    etc ...
}

[String:Any] 过去曾给我带来过问题。无论如何,提供您尝试阅读的数据样本会很有帮助。

【讨论】:

  • 请原谅我,但我仍然对 qrCode 的来源感到困惑。我在您拥有的示例 JSON 中没有看到它。我认为是二维码让你头疼。
  • 'test9'、'test10'、'test11'等是qrCode的值
【解决方案2】:

虽然代码应该打印,但无论如何它看起来都是一种糟糕的方法。您正在将所有二维码从数据库下载到客户端,然后检查其中一个是否存在。数据库中的数据越多,每次检查下载的数据就越多。在数据库中将观察者附加到低一级会更有效:

func checkForDuplicateScan(qrCode: String) {
  DataService.ds.REF_SAMPLES.child(qrCode).observeSingleEvent(of: .value, with: { (snapshot) in
    if snapshot.exists() {
        if let sampleDict = snapshot.value as? [String:Any] {
            if let isScanned = sampleDict["scanned"] as? Bool {
                if isScanned == true {

【讨论】:

  • 可选()这就是我得到的
  • 是的,听起来我们缺少了一些东西。让我们尝试不同的方法。我更新了我的答案,以展示一种更有效的方法。
  • 还是不行。想不出为什么它不起作用的原因。 :|
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 2018-01-24
  • 1970-01-01
  • 2017-04-07
  • 2017-02-10
  • 2018-01-29
  • 1970-01-01
相关资源
最近更新 更多