【问题标题】:Working with Firebase and some Date filters使用 Firebase 和一些日期过滤器
【发布时间】:2017-02-05 12:09:38
【问题描述】:

我有一个酒店预订应用程序,它向用户显示可滑动的日历。现在的最终目标是提供一个类似于日历的界面,在每个保留的日期下方都有小点。现在,我只是想在控制台上打印开始日期和结束日期之间的所有预订(其中开始日期和结束日期分别是月份的开始和结束)。

但是,我无法让它与 Firebase 一起使用。以下是目前 Firebase 上的数据:

{
  "reservations" : {
    "-KSgRjwpssoZJWjV9ScM" : {
      "checkin" : 1474950600,
      "checkout" : 1475116200,
      "customer" : "-KMVMMudWJlFeiimtgJl"
    }
  }
}

这是我检索数据的 Swift 代码:

private var ref: FIRDatabaseReference!

// viewdidload:
ref = FIRDatabase.database().reference()
ref.keepSynced(true)

// logic to fetch data
DispatchQueue.global().async {

        // Background Processing
        // Check Firebase for events between startdate and enddate
        print("Fetching Reservations")
        print(startDate.timeIntervalSince1970)
        print(endDate.timeIntervalSince1970)

        self.ref.child("reservations").queryStarting(atValue: startDate.timeIntervalSince1970, childKey: "checkin").observe(.childAdded, with: { (snapshot) -> Void in

            print("Data Retrieved.\n")
            print(snapshot)

        })
}

这是现在打印到控制台上的内容:

Fetching Reservations
1469989800.0
1472581800.0
Fetching Reservations
1472668200.0
1475173800.0

由于数据在第二个范围内清晰可见,但我仍然无法显示。

这里可能有什么问题?谢谢。

【问题讨论】:

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


    【解决方案1】:

    我已尝试使用此代码并且它有效:

    let ref = FIRDatabase.database().reference()
    let refReservations = ref.child("reservations")
    
    let startDate: Double = 1472668200.0
    
    refReservations
      .queryOrdered(byChild: "checkin")
      .queryStarting(atValue: startDate)
      .observeSingleEvent(of: .value) { (snap: FIRDataSnapshot) in
        print(snap.value)
      }
    

    你的代码和我的不同之处在于我多调用一个查询:

    .queryOrdered(byChild: "checkin")
    .queryStarting(atValue: startDate)
    

    你的:

    .queryStarting(atValue: startDate, childKey: "checkin")
    

    【讨论】:

    • 我之所以使用.queryStarting(atValue: startDate, childKey: "checkin") 是因为我需要根据月份加载预订。因此,当用户滑动到不同的月份时,我会查询 firebase 的预订 starting at x dateending at y date - 键是 checkincheckout。所以你建议的方法也会给我预订,比如 9 月,8 月,因为当然时间戳更大(与 checkin 相比)。
    • 但是添加到您的答案中,我通过扩展您的查询使其工作:self.ref.queryOrdered(byChild: "checkin").queryStarting(atValue: startDate.timeIntervalSince1970).queryEnding(atValue: endDate.timeIntervalSince1970, childKey: "checkout").observe(.childAdded, with: { (snapshot) -> Void in print(snapshot) })
    猜你喜欢
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多