React Native 需要MainQueueSetup和dispatch_get_main_queue的区别?

xoshrz7s  于 2023-08-07  发布在  React
关注(0)|答案(3)|浏览(99)

我试图学习如何为iOS创建rect-native模块,有一个方面出现了
关于线程的官方文档提到了这段代码及其变体

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

字符串
还有另一个未记录的和平,我看到了很多在第三方图书馆,这是

+ (BOOL)requiresMainQueueSetup
{
    return NO;
}


对我来说,这些看起来有点相似,但又有所不同,因此我想请你解释以下问题

  1. dispatch_get_main_queue应该什么时候添加到模块中,如果省略它会发生什么?
  2. requiresMainQueueSetup应该什么时候添加到模块中,如果省略了会发生什么?
  3. dispatch_get_main_queuerequiresMainQueueSetup可以一起使用吗?如果可以,为什么?什么时候?
    1.从requiresMainQueueSetup返回YESNO有什么区别?
fae0ux8s

fae0ux8s1#

1.每当您在次线程上处理将影响主线程的事件时,都需要dispatch_get_main_queue()。通常,这涉及UI更改。如果您正在创建一个不涉及本机渲染的React Native模块,则可能不需要使用主队列。异步的东西应该在一个辅助线程上被调用,这就是你要实现dispatch_get_main_queue()的地方,以确保当你的异步操作完成时,你的UI得到更新。
1.几周前我在SO上问过同样的问题,但没有成功,经过一些研究,我现在知道这与1号子弹有关。React-native希望您实现此方法(与iOS没有任何关系),如果您希望进行原生iOS渲染,则需要返回YES。这将确保本地模块在与UI交互相关的主线程上运行。您不希望应用程序在繁重的处理任务中冻结您的UI。
1.如果您不提供requiresMainQueueSetup(),react-native会在您面前抛出一个警告,但此时会将其设置为YES。此默认值将在即将发布的版本中更改为“否”。所以我来回答你的问题:它们可以一起使用,但不是每个组合都有意义。同样在这种情况下,如果您不创建新的本地iOS UI组件,则可能不需要通过dispatch_get_main_queue()访问主线程。react-native桥将确保本机事件和方法始终在iOS和JS之间进行通信,反之亦然,无论它们在哪个线程上运行。
1.这一点已在前面的要点中解决
编辑:一些额外的信息,只是为了确保一切都清楚。总结如下:requiresMainQueueSetup()与iOS无关,它只是由react-native创建的,以了解您的本地模块(UI或其他)的意图。dispatch_get_main_queue()与react-native无关,它只与您的本机代码相关。它基本上是次线程的回调,用于通知主线程某些异步操作已完成。

myzjeezk

myzjeezk2#

1.dispatch_get_main_queue 应该在本地模块的方法需要在运行时访问UI(主要是)时添加。它可以放在instant methodQueue中,或者其他解决方案是 Package 一个代码块,例如 dispatch_async(dispatch_get_main_queue(),^ {
这里有一些代码})

1.requiresMainQueueSetup 是一个类方法(用+号表示),它只在初始化时工作。因此,如果 init 方法正在调用UI或您覆盖 constantToExport 方法,则需要它。
1.这一问题已在上文中讨论过。
1.这一问题已在上文中讨论过。

hgncfbus

hgncfbus3#

在React Native库提供的'RCTBridgeModule.h'文件中有关于requiresMainQueueSetupmethodQueue的官方文档,它还包含react bridge模块的其他文档功能。

RCTBridgeModule.h

/**
 * The queue that will be used to call all exported methods. If omitted, this
 * will call on a default background queue, which is avoids blocking the main
 * thread.
 *
 * If the methods in your module need to interact with UIKit methods, they will
 * probably need to call those on the main thread, as most of UIKit is main-
 * thread-only. You can tell React Native to call your module methods on the
 * main thread by returning a reference to the main queue, like this:
 *
 * - (dispatch_queue_t)methodQueue
 * {
 *   return dispatch_get_main_queue();
 * }
 *
 * If you don't want to specify the queue yourself, but you need to use it
 * inside your class (e.g. if you have internal methods that need to dispatch
 * onto that queue), you can just add `@synthesize methodQueue = _methodQueue;`
 * and the bridge will populate the methodQueue property for you automatically
 * when it initializes the module.
 */
@property (nonatomic, strong, readonly) dispatch_queue_t methodQueue;

/**
 * Most modules can be used from any thread. All of the modules exported non-sync method will be called on its
 * methodQueue, and the module will be constructed lazily when its first invoked. Some modules have main need to access
 * information that's main queue only (e.g. most UIKit classes). Since we don't want to dispatch synchronously to the
 * main thread to this safely, we construct these modules and export their constants ahead-of-time.
 *
 * Note that when set to false, the module constructor will be called from any thread.
 *
 * This requirement is currently inferred by checking if the module has a custom initializer or if there's exported
 * constants. In the future, we'll stop automatically inferring this and instead only rely on this method.
 */
+ (BOOL)requiresMainQueueSetup;

字符串

相关问题