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
When should
dispatch_get_main_queuebe added to the module and what happens if it is omitted?When should
requiresMainQueueSetupbe added to the module and what happens if it is omitted?Can
dispatch_get_main_queueandrequiresMainQueueSetupbe used together, if so why and when?What is the difference between returning
YESandNOfromrequiresMainQueueSetup?
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.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.
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 throughdispatch_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
Edit:
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.
来源:https://stackoverflow.com/questions/50773748/difference-requiresmainqueuesetup-and-dispatch-get-main-queue