问题描述
我正在尝试了解如何为 iOS 创建 react-native 模块,并且出现了一个方面
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
什么时候应该将
dispatch_get_main_queue
添加到模块中,如果省略会发生什么?
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_queue
和 requiresMainQueueSetup
是否可以一起使用,如果可以,为什么以及何时?
Can dispatch_get_main_queue
and requiresMainQueueSetup
be used together, if so why and when?
requiresMainQueueSetup
返回YES
和NO
有什么区别?
推荐答案
每当您在辅助线程上处理会影响主线程的事件时,您都需要
dispatch_get_main_queue()
.通常这涉及 UI 更改.如果你正在创建一个不涉及原生渲染的 react-native 模块,你可能不需要主队列.应该在辅助线程上调用异步内容,您可以在此处实现dispatch_get_main_queue()
以确保在异步操作完成时更新 UI.
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 implementdispatch_get_main_queue()
to make sure you UI gets updated when you async actions are completed.
几周前我在 SO 上问了同样的问题,但没有成功,经过一些研究,我现在知道这与第 1 号项目符号有关.React-native 期望你实现这个方法(无论如何都不与 iOS 相关),您需要返回 YES 才能进行原生 iOS 渲染.这将确保您的本机模块在与 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!