【问题标题】:Swift Retreive Firebase dataSwift 检索 Firebase 数据
【发布时间】:2017-01-04 05:27:38
【问题描述】:

Firebase 数据库中的以下数据:

{
  "schedule": {
      "Week 1": {
        "active": "true"
      },
      "Week 2": {
        "active": "true"
      },
      "Week 3": {
        "active": "false"
      },
      "Week 4": {
        "active": "true"
      },  
      ...
   }
}

我想检索所有只有 active 为 true 的 Weeks。 我正在这样设置 Firebase 参考:

ref = FIRDatabase.database().reference(withPath: "Schedule")

然后执行以下操作:

ref.observeSingleEvent(of: .value, with: { snapshot in
        //
        print("Count: ", snapshot.childrenCount)

        for _ in snapshot.children {
                print("\(snapshot.value)")
        }

    })

产生以下输出:

Optional({
      "Week 1" =     {
           active = 1;
       };
       "Week 2" =     {
           active = 1;
       };
       "Week 3" =     {
           active = 0;
       };
       "Week 4" =     {
           active = 1;
       };
       ...
}

有人可以建议我如何获得将 active 设置为 true 的几周,这需要加载到 string 类型的数组中:

var weeksActive = [String]()

【问题讨论】:

  • 这在 Firebase 入门指南 Working With Lists 的过滤数据部分中进行了介绍。但是,旧的 Firebase 文档比当前的要好得多,因此请查看查询数据部分中的 Retrieving Data。它们已经过时了,但解释和示例会有所帮助。

标签: ios swift firebase swift3 firebase-realtime-database


【解决方案1】:

为此,您可以像这样使用queryOrdered(byChild:)queryEqual(toValue:)

let ref = FIRDatabase.database().reference(withPath: "Schedule").queryOrdered(byChild: "active").queryEqual(toValue : true)

ref.observe(.value, with:{ (snapshot) in

    print("Count: ", snapshot.childrenCount)
    for child in snapshot.children {
        print("\((child as! FIRDataSnapshot).value)")
        //To get week you need to access key
        print("\((child as! FIRDataSnapshot).key)")
    }
})

【讨论】:

  • 输出如下: Count: 4 Optional({ active = 1; }) Optional({ active = 1; }) Optional({ active = 0; }) Optional({ active = 1 ; })
  • 输出如下: Count: 4 Optional({ active = 1; }) Optional({ active = 1; }) Optional({ active = 0; }) Optional({ active = 1 ; }) 我希望得到一个这样的列表:第 1 周、第 2 周、第 4 周,因为这些是那些将 active 设置为 true 而不是 active = 1
  • @iosforme 检查编辑的答案,您可以使用密钥获得一周。如果您仍然遇到问题,那么您需要在 Schedule 中显示您是如何添加该孩子的。
  • @iosforme 欢迎朋友 :)
【解决方案2】:

这是代码

1) 在名称为Schedule.swift 中创建新的 Swift 页面

2) 在Schedule.swift 页面内使用以下代码

import UIKit
class Schedule: NSObject
{
   var weeks: String?
   var active: String?
}

3) 在ViewController.Swift 页面或其他页面使用此代码

import UIKit
import Firebase
import FirebaseDatabase
class ViewController: UIViewController
{
    var ShceduleList = [Schedule]()
    var ref = FIRDatabaseReference!
    var refHandle: Uint!
    override func viewDidLoad()
    {
       super.viewDidLoad()
       ref = FIRDatabase.database().reference()
       self.FetchSchedule()
    }

    fun FetchSchedule()
    {
       refHandle = ref.child("schedule").observeEventType(.ChildAdded, withBlock: {(snapshot) in
          if let dictionary = snapshot.value as? [String: AnyObject]
          {
               let Schedule = Schedule()
               Schedule.setValuesForKeyWithDictionary(dictionary)
               self.ScheduleList.append(Schedule)
               dispatch_async(dispatch_get_main_queue(),{
                print("Schedule List: \(self.ScheduleList)")
               })
          } 
       })
    }
}

在这里,我已经添加了将数据附加到数组的代码,您只需在附加到 ScheduleList 数组之前根据您的需要验证值。

【讨论】:

  • 这不是问题的答案,OP 希望周活动是真的,你所做的只是观察ChildAdded
【解决方案3】:

这是解决上述问题的代码。首先将项目值作为 FIRDatabaseSnapshot.value!。然后将其附加到 weekActive 中。

    ref.observeSingleEvent(of: .value, with: { snapshot in
            //
            print("Count: ", snapshot.childrenCount)

            for item in snapshot.children {
                    print("\(item as! FIRDataSnapshot).value)")
                   let week = item as! FIRDataSnapshot).value!

            }

        })

【讨论】:

  • 不。不正确,也没有解决 OP 问题。
  • 什么是正确的地址请解释一下。它会帮助别人。
  • 问题问 我想检索所有只有 active 为 true 的 Weeks。 这个答案检索 ref 中的所有节点,然后遍历它们。它不会只返回 active 为 true 的那些。
猜你喜欢
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多