Firebase异步功能,后台队列中有什么,什么不是 [英] Firebase asynchronous function, what's in the background queue and what's not

查看:99
本文介绍了Firebase异步功能,后台队列中有什么,什么不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有这个

  //一堆代码... 
FIRDatabase.database.reference ).Schedule(somechild)。observeSingleEventOfType(.Value,withBlock {(snapshot)in

//完成处理程序中的一些代码})

请不要太多地阅读代码片段,询问更多的代码,我只需要简短的回答。它说,一堆代码,假设它实际上是在主队列上的一些代码。现在,当我写下observeSingleEvent Firebase完成处理程序时,我的分析就是这样。 observeSingleEvent获取后台队列上的快照,以阻止主队列上的UI。

  • 一旦获取快照,in之后的代码块现在回到MAIN队列,所以可以将任何与UI相关的代码放入我的整个应用程序是基于这个推理,所以如果我不正确,请告诉我什么是实际的继续。这确实是Firebase数据库客户端的工作方式:所有的网络和磁盘I / O都发生关闭

    主线程,那么你的回调/块在主线程上调用


    Lets say I have this

    // a bunch of code...  
    FIRDatabase.database.reference.child("somechild").observeSingleEventOfType(.Value, withBlock{(snapshot) in   
    
    //some code inside of the completion handler})
    

    Please don't read too much into the code snippet asking for more code, I only require a short answer. Where it says, "a bunch of code", assume that its actually some code that's on the MAIN queue. Now, when I write down the observeSingleEvent Firebase completion handler, my analysis on it is this.

    1. observeSingleEvent fetches the snapshot on a background queue as to not block UI thats on the main queue.

    2. Once it fetches the snapshot, the block of code after "in" is now back to the MAIN queue, so its fine to put any UI related code in there.

    My whole app is based on this reasoning, so if I'm incorrect, please tell me what's actually going on.

    解决方案

    That's indeed how the Firebase Database client works: all network and disk I/O happen off the main thread, then your callbacks/blocks are invoked on the main thread.

    这篇关于Firebase异步功能,后台队列中有什么,什么不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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