简体   繁体   English

离线时,Firebase值事件不会一直触发

[英]Firebase value event don't get triggered all the time while offline

I have the following path pattern: 我有以下路径模式:

/ID_Company/boxes/timestamp_of_the_day/ID_box

Let's say I just started a new day and I'm offline. 假设我刚开始新的一天,我就离线了。 Right now on Firebase DB, the path /ID_Company/boxes/timestamp_of_TODAY doesn't exist, neither in the cache. 目前在Firebase DB上,路径/ID_Company/boxes/timestamp_of_TODAY在缓存中都不存在。

No I add a new box to the path /ID_Company/boxes/timestamp_of_TODAY/id_box1 不,我在路径/ID_Company/boxes/timestamp_of_TODAY/id_box1添加了一个新框

If I have an observer on childAdded event, it will be triggered. 如果我在childAdded事件上有一个观察者,它将被触发。 But if I have an observer on value event, nothing is triggered. 但是如果我有value事件的观察者,则不会触发任何事件。

Now let say that I was online when I added the first box. 现在让我说,当我添加第一个框时,我在线。 So on firebase this path /ID_Company/boxes/timestamp_of_TODAY/id_box1 exists and so it does locally. 因此在/ID_Company/boxes/timestamp_of_TODAY/id_box1上,此路径/ID_Company/boxes/timestamp_of_TODAY/id_box1存在,因此它在本地执行。

It go offline. 它离线了。 And I add a new box on /ID_Company/boxes/timestamp_of_TODAY/id_box2 , then 'value` event is triggered and I just don't understand why. 我在/ID_Company/boxes/timestamp_of_TODAY/id_box2上添加了一个新框,然后'value`事件被触发,我只是不明白为什么。

Why is it triggered when timestamp_of_TODAY already exists but not when it doesn't? 为什么在timestamp_of_TODAY已经存在时触发但是在不存在时触发?

Thanks for your help. 谢谢你的帮助。

EDIT: 编辑:

Here is how I add a box: 以下是我添加方框的方法:

        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)
            }
        }

And here is how I get boxes: 以下是我如何获得盒子:

    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())
    })

I am sure but please try below. 我很确定,但请在下面尝试。 Might be work for you. 可能会为你工作。

guard let startingTimestamp = day.beginning(of: .day)?.timeIntervalSince1970, let endingTimestamp = day.end(of: .day)?.timeIntervalSince1970 else { return 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(.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())
})

Look at this document: FIRDataEventTypeValue 请查看此文档: FIRDataEventTypeValue

You can use the FIRDataEventTypeValue event to read the data at a given path, as it exists at the time of the event 您可以使用FIRDataEventTypeValue事件来读取给定路径上的数据,因为它在事件发生时存在

Hope it can help you. 希望它可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM