Swift Retreive Firebase数据

时间:2017-01-04 05:27:39

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

Firebase数据库中的以下数据:

(defn my-count [sq]
 (letfn [(inner-count [c s]
          (if (empty? s) 
           c
           (recur (inc c) (rest s))))]
  (inner-count 0 sq)))

我想检索活动为真的所有周。 我正在设置Firebase参考:

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

然后执行以下操作:

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

产生以下输出:

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

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

    })

有人可以建议我如何才能获得active设置为true的周数,需要加载一个字符串类型数组:

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

3 个答案:

答案 0 :(得分:4)

为此,您可以像这样使用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)")
    }
})

答案 1 :(得分:0)

这是守则

1)在名称中创建新的Swift页面Schedule.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数组之前根据需要验证值。

答案 2 :(得分:-1)

这是上述问题的代码。首先将项目值作为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!

            }

        })
相关问题