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

标签 ios swift firebase firebase-realtime-database

我有以下路径模式:

/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 上添加了一个新框,然后触发了“值”事件,我只是不明白为什么。

为什么 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())
    })

最佳答案

看这个文档: FIRDataEventTypeValue

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

希望对您有所帮助。

关于ios - 离线时不会一直触发 Firebase 值事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42386692/

相关文章:

iphone - UICollectionView 自定义布局 - 单元格未正确排列

ios - 如何在 Objective C 中创建 CBUUID

ios - 一段时间后如何显示 UIAlertView?

Firebase REST API 持久连接,速度优化

ios - 如何在 iOS 中暂停/恢复/取消事件

iOS UIWebView 无法加载

ios - 从 FBSDK graph Api (V 2.5) 获得用户喜欢

json - 内部 JSON 结构与 Decodable 方法冲突

java - 应用程序因向 FirebaseDatabase.getReference() 传递空值而崩溃

ios - Swift/iOS FirebaseGoogleSignIn OAuth 客户端 ID 未找到