区别需要MainQueueSetup和dispatch_get_main_queue吗? [英] Difference requiresMainQueueSetup and dispatch_get_main_queue?

查看:95
本文介绍了区别需要MainQueueSetup和dispatch_get_main_queue吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习为iOS创建本机模块,并且出现了一个方面

I am trying to learn about creating react-native modules for iOS and there is one aspect that came up

关于线程的官方文档提到了这一点代码块及其变体

Official documentation on threading mentions this block of code alongside its variations

- (dispatch_queue_t)methodQueue
{
  return dispatch_get_main_queue();
}

我在第三方库中看到了另一种没有证件的和平,这是

There is another undocumented peace I saw a lot in the third party libraries which is this

+ (BOOL)requiresMainQueueSetup
{
    return NO;
}

在我看来,这些看起来有些相似但又有所不同,因此我想对以下问题进行解释

To me, these look kinda similar yet different, hence I wanted to ask for an explanation of following questions

  1. 何时应将dispatch_get_main_queue添加到模块中,如果将其省略,会发生什么情况?

  1. When should dispatch_get_main_queue be added to the module and what happens if it is omitted?

何时应将requiresMainQueueSetup添加到模块中,如果将其省略,会发生什么情况?

When should requiresMainQueueSetup be added to the module and what happens if it is omitted?

dispatch_get_main_queuerequiresMainQueueSetup是否可以一起使用,如果可以,为什么以及何时使用?

Can dispatch_get_main_queue and requiresMainQueueSetup be used together, if so why and when?

requiresMainQueueSetup返回YESNO有什么区别?

推荐答案

  1. 每当在辅助线程上处理将影响主线程的事件时,都需要dispatch_get_main_queue().通常,这涉及UI更改.如果要创建不涉及本机渲染的react-native模块,则可能不需要主队列.异步内容应在辅助线程上调用,这是实现dispatch_get_main_queue()的地方,以确保在异步操作完成后更新UI.

  1. You need dispatch_get_main_queue() whenever you are processing events on a secondary thread which will influence the main thread. Typically this involves UI changes. If you are creating a react-native module which does not involve native rendering you will probably not need the main queue. Async stuff should be invoked on a secondary thread, and this is where you would implement dispatch_get_main_queue() to make sure you UI gets updated when you async actions are completed.

几周前,我在SO上问了同样的问题,但没有成功,经过一些研究,我现在知道这与项目符号1有关.React-native希望您实现此方法(无论如何) (与iOS相关),并且您想要执行本机iOS渲染时需要返回YES.这将确保您的本机模块在与UI交互有关的主线程上运行.您不希望应用程序在繁重的处理过程中冻结UI.

I asked this same question on SO a few weeks ago without success, and after some research I now know this is related to bullet number 1. React-native expects you to implement this method (is not in any way related to iOS), and you will need return YES in you want to do native iOS rendering. This will ensure that your native module is run on the main thread which is relevant in case of UI interactions. You don't want the application to freeze your UI in case of heavy duty processing.

如果不提供requiresMainQueueSetup(),则react-native会向您发出警告,但此时将其设置为YES.该默认值将在即将发布的版本中更改为NO.因此,请回答您的问题:它们可以一起使用,但并非每种组合都有意义.同样在这种情况下,如果您不创建新的本机iOS UI组件,则可能不需要通过dispatch_get_main_queue()访问主线程. react-native桥将确保始终将本地事件和方法从iOS传递到JS,反之亦然,无论它们在哪个线程上运行.

If you do not provide requiresMainQueueSetup() react-native will throw a warning in your face, but will set it to YES at this point. This default value will change in an upcoming release to NO. So to answer your question: they can be used together, but not every combination makes sense. Also in this case, if you are not creating a new native iOS UI component you will probably not need to access the main thread through dispatch_get_main_queue(). The react-native bridge will ensure that native events and methods are always communicated from iOS to JS and visa versa, regardless of which thread they are running on.

在先前的项目符号中已经解决了此问题

This has been addressed in the previous bullets

一些其他信息只是为了确保一切都清楚.总结一下:requiresMainQueueSetup()与iOS无关,仅由react-native创建,以了解您的本机模块(UI或其他)的意图. dispatch_get_main_queue()与react-native无关,仅与您的本机代码有关.基本上,它是辅助线程的回调,用于通知主线程一些异步操作已完成.

Some additional information just to make sure everything is clear. To summarise: requiresMainQueueSetup() has nothing to do with iOS, and is only created by react-native to know what the intentions of your native module are (UI or other). dispatch_get_main_queue() has nothing to do with react-native and is only relevant to your native code. It is basically a callback for secondary threads to inform the main thread that some async actions are completed.

这篇关于区别需要MainQueueSetup和dispatch_get_main_queue吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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