【问题标题】:Firebase value event don't get triggered all the time while offline离线时不会一直触发 Firebase 值事件
【发布时间】:2017-07-12 05:15:37
【问题描述】:

我有以下路径模式:

/ID_Company/boxes/timestamp_of_the_day/ID_box

假设我刚刚开始新的一天,但我处于离线状态。目前在 Firebase DB 上,路径 /ID_Company/boxes/timestamp_of_TODAY 不存在,缓存中也不存在。

不,我在路径 /ID_Company/boxes/timestamp_of_TODAY/id_box1 中添加了一个新框

如果我有childAdded 事件的观察者,它将被触发。但是如果我在value 事件上有观察者,则不会触发任何事情。

现在假设我在添加第一个框时在线。所以在firebase上这条路径/ID_Company/boxes/timestamp_of_TODAY/id_box1存在,所以它在本地存在。

它离线了。我在/ID_Company/boxes/timestamp_of_TODAY/id_box2 上添加了一个新框,然后触发了 'value` 事件,我就是不明白为什么。

为什么timestamp_of_TODAY已经存在时触发,不存在时不触发?

感谢您的帮助。

编辑:

这是我添加框的方法:

        guard let startingTimestamp = date.beginning(of: .day)?.timeIntervalSince1970 else { return nil }

        let boxRef = dbRef.child("ID_Company").child("boxes").child("\(startingTimestamp)").childByAutoId()

        var box = box
        box.id = boxRef.key

        boxRef.setValue(box.toDictionary()) { error, ref in
            if let error = error as? NSError {
                print(error)
                completion(error)
            } else {
                completion(nil)
            }
        }

这是我获得盒子的方法:

    guard let startingTimestamp = day.beginning(of: .day)?.timeIntervalSince1970, let endingTimestamp = day.end(of: .day)?.timeIntervalSince1970 else { return nil }

    let boxesRef = dbRef.child("ID_Company").child("boxes").child("\(startingTimestamp)")

    let query = boxesRef.queryOrdered(byChild: Box.Key.dateTimestamp.rawValue).queryStarting(atValue: startingTimestamp).queryEnding(atValue: endingTimestamp + 0.001)

    let handle = query.observe(.value, with: { snapshot in
        var boxes: [Box] = []

        for child in snapshot.children {
            let box = Box(snapshot: child as! FIRDataSnapshot)

            if userID == nil || box.userID == userID! {
                boxes.append(box)
            }
        }

        completion(boxes.reversed())
    })

【问题讨论】:

  • 一些代码怎么样?你能用用于将观察者附加到节点的代码更新你的问题吗?
  • @Jay 我刚刚编辑了问题
  • 我想从你的问题中理解这句话。 “如果我在 childAdded 事件上有一个观察者,它将被触发。但如果我在 value 事件上有一个观察者,则不会触发任何事情。” .在哪条路径上有调用的 childAdded 事件,在哪条路径上有没有调用的 value 事件?

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

我确定,但请在下面尝试。可能对你有用。

guard let startingTimestamp = day.beginning(of: .day)?.timeIntervalSince1970, letendingTimestamp = day.end(of: .day)?.timeIntervalSince1970 else { return nil }

let boxesRef = dbRef.child("ID_Company").child("boxes").child("\(startingTimestamp)")

let query = boxesRef.queryOrdered(byChild: Box.Key.dateTimestamp.rawValue).queryStarting(atValue: startingTimestamp).queryEnding(atValue: endingTimestamp + 0.001)

let handle = query.observe(.ChildAdded, with: { snapshot in
    var boxes: [Box] = []

    for child in snapshot.children {
        let box = Box(snapshot: child as! FIRDataSnapshot)

        if userID == nil || box.userID == userID! {
            boxes.append(box)
        }
    }

    completion(boxes.reversed())
})

【讨论】:

    【解决方案2】:

    看这个文件: FIRDataEventTypeValue

    您可以使用 FIRDataEventTypeValue 事件读取给定路径上的数据,因为它在事件发生时存在

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-10-02
      • 1970-01-01
      • 2021-01-26
      • 2019-12-04
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      相关资源
      最近更新 更多