Firebase观察者实际上是做什么的? [英] What does a Firebase observer actually do?

查看:41
本文介绍了Firebase观察者实际上是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说这个观察者是在应用启动时创建的

Say this observer is created when the app is launched

ref.observe(.value, with: { (snapshot) in
    // do something
})

幕后发生了什么?当值更改时,应用程序如何知道?我只能想象的是

What is going on behind the scenes? How does the app know when the value changes? All I can imagine is this

while (true) {
    ref.observeSingleEvent(of: .value, with: { (snapshot) in
        // if value is different do something
    })
}

推荐答案

Firebase客户端保持与后端服务器的开放套接字连接.当您附加观察者时,它会将信息发送到服务器,服务器将从此开始跟踪观察到的位置.服务器还会在位置上发回当前值(或缺少当前值).客户将立即使用该值触发/调用您的完成区块.

The Firebase client keeps an open socket connection to its backend server. When you attach the observer, it sends that information to the server, which will from then on track the observed location. The server also sends back the current value (or lack thereof) on the location. The client will immediately fire/invoke your completion block with that value.

此初始流程之后,只要位置的值发生更改,Firebase服务器就会通过打开的套接字将更新发送给客户端.当客户端收到这样的更新时,它将使用更新后的值(或缺少更新后的值)再次调用完成块.

After this initial flow, whenever the value at the location changes, the Firebase server sends an update to the client over the open socket. When the client receives such an update, it invokes the completion block again with the updated value (or lack thereof).

这篇关于Firebase观察者实际上是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆