Difference requiresMainQueueSetup and dispatch_get_main_queue?
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.
dispatch_get_main_queue should be added when your native module’s methods need access to UI (primarily) at runtime. It can be placed in instant methodQueue or other solution is to wrap a block of code such as *dispatch_async( dispatch_get_main_queue(), ^ {
some code here })*
requiresMainQueueSetup is a class method (indicated by + sign) and it works only at initialization time. So it is needed if your init method is calling UI or you override constantToExport method.
It is addressed above.
It is addressed above.