【问题标题】:how to retrieve child(array) inside another firebase child如何在另一个firebase子项中检索子项(数组)
【发布时间】:2016-11-11 11:03:25
【问题描述】:

我正在尝试从firebase 打印array。实际上,如果我们点击列表中的药物(tableviewcontroller),它会显示其特定剂量。我被困在检索剂量清单上。这是我从 firebase 获取数据的代码。任何帮助表示赞赏。提前致谢。我的 firebase 结构看起来像这样.. firebase img

 func loadDataFromFirebase() {


    databaseRef = FIRDatabase.database().reference().child("medication")


    databaseRef.observeEventType(.Value, withBlock: { snapshot in


        for item in snapshot.children{
        FIRDatabase.database().reference().child("medication").child("options").observeEventType(.Value, withBlock: {snapshot in
                print(snapshot.value)
            })
        }

    })

【问题讨论】:

    标签: ios arrays swift firebase


    【解决方案1】:

    您应该查看 firebase 文档 https://firebase.google.com/docs/database/ios/read-and-write

    但如果我理解你的想法,你可能有一个用于药物的模型类。因此,要检索您的数据,您应该在 Swift 3.0 中这样做:

    func loadDataFromFirebase() {
    
    
    databaseRef = FIRDatabase.database().reference().child("medication")
    
    
    databaseRef.observe(.value, with: { (snapshot) in
    
        for item in snapshot.children{
        // here you have the objects that contains your medications
        let value = item.value as? NSDictionary
    
        let name = value?["name"] as? String ?? ""
        let dossage = value?["dossage"] as? String ?? ""
        let type = value?["type"] as? String ?? ""
        let options = value?["options"] as? [String] ?? ""
    
        let medication = Medication(name: name, dossage: dossage, type: type, options: options)
        // now you populate your medications array
        yourArrayOfMedications.append(medication)
        }
    
      yourTableView.reloadData()
    })
    }
    

    现在您已经拥有了包含所有药物的数组,您只需使用这些药物填充您的 tableView。当有人按下桌子上的物品时,您只需拨打 prepareForSegue: 并将您的 yourArrayOfMedications[indexPath.row].options 发送到下一个视图

    【讨论】:

    • 如果我们选择其药物名称,我想在单独的表格视图中打印该“选项”。问题是在数组中..所以将其作为字符串检索会显示错误...卡在这里很长时间..
    【解决方案2】:
    • 解决方法与上面相同,但略有不同。

      func loadDataFromFirebase() {
        databaseRef = FIRDatabase.database().reference().child("medication")
        databaseRef.observe(.value, with: { (snapshot) in
      
        for item in snapshot.children{
        // here you have the objects that contains your medications
        let value = item.value as? NSDictionary
      
        let name = value?["name"] as? String ?? ""
        let dossage = value?["dossage"] as? String ?? ""
        let type = value?["type"] as? String ?? ""
        let options = value?["options"] as? [String : String] ?? [:]
      
        print(options["first"]) // -> this will print 100 as per your image
        // Similarly you can add do whatever you want with this data
      
        let medication = Medication(name: name, dossage: dossage, type: type, options: options)
        // now you populate your medications array
        yourArrayOfMedications.append(medication)
      }
      
      yourTableView.reloadData()
      })
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 2022-11-22
      • 2018-06-05
      • 2021-12-25
      • 2021-01-09
      • 2018-01-06
      相关资源
      最近更新 更多