【问题标题】:Firebase query not being firedFirebase 查询没有被触发
【发布时间】:2016-12-23 16:04:42
【问题描述】:

我在整个查询中都为 Firebase 设置了断点,所发生的只是下一行的断点被命中(这是第一行),然后没有其他的。

_CHAT_REF.observeEventType(.Value, withBlock: { snapshot in

知道为什么会这样吗?即使没有数据,查询块内的断点应该仍然会被命中,但事实并非如此。我做了什么:我已经卸载并重新安装了 Firebase 至少 10 次,使用 CocoaPods 而不是 CocoaPods,按照 T 的指示。我没有收到任何类型的编译错误,我正在运行 FIRApp.configure () 在我的应用程序委托中。

完整代码(每一行都有断点,没有只调用 _CHAT_REF.observe 行):

private var _CHAT_REF = FIRDatabase.database().reference().child("chats")

_CHAT_REF.observeEventType(.Value, withBlock: { snapshot in

    self.individualMessages = []

    if snapshot.hasChildren() {
        // Found chats
        for snap in snapshot.children {
            let theChat = Chat(snapshot: snap as! FIRDataSnapshot)

            // The current user belongs to this chat, so add it to individual messages.
            if theChat.sender_id == GlobalEnv.currentUser.id || theChat.receiver_id == GlobalEnv.currentUser.id {
                self.individualMessages.append(theChat)
            }
        }
    } else {
        // No Children
        print("No children found.")
    }
    self.tvContacts.reloadData()
})

数据库结构:

DB Structure on Firebase

【问题讨论】:

  • _CHAT_REF 长什么样子?
  • 私有变量 _CHAT_REF = FIRDatabase.database().reference().child("chats")
  • 你的数据库结构是什么样的?
  • 用数据库结构的链接编辑了我的原始帖子,感谢您的帮助。
  • 我认为这段代码不在didFinishLaunchingFIRApp.configure 中?立即查询有时会导致问题

标签: swift firebase firebase-realtime-database


【解决方案1】:

我遇到了类似的问题。事实证明,我无法从组织的代理服务器后面读取/写入数据库。我使用开放 wifi 构建到一个设备上,它工作正常。

【讨论】:

  • 我似乎能够从我使用 Firebase 的其他应用程序中读取/写入。只是这个,一个我添加 Firebase 的旧 Swift 项目,我遇到了问题。我刚才也尝试了其他设备和wifi,没有运气。
【解决方案2】:

试试这个,如果有什么不同,请告诉我。它非常简化,但变量分配的处理方式不同。

假设你在一个 ViewController 类中......

class ViewController: UIViewController {
    var ref: FIRDatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()
        ref = FIRDatabase.database().reference()
    }

    func clickAction() {  //just tied to an action in the UI
       let chatsRef = ref.child("chats")
       chatsRef.observeEventType(.Value, withBlock: { snapshot in
         print("Hello, World")
       })
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2012-06-03
    • 2019-11-07
    相关资源
    最近更新 更多