Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference requiresMainQueueSetup and dispatch_get_main_queue?

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. When should dispatch_get_main_queue be added to the module and what happens if it is omitted?

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

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

  4. What is the difference between returning YES and NO from requiresMainQueueSetup?

like image 690
Ilja Avatar asked Jun 09 '18 11:06

Ilja


2 Answers

  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.

  2. 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.

  3. 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.

  4. 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.

like image 67
dentemm Avatar answered Sep 21 '22 15:09

dentemm


  1. 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 })*

  2. 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.

  3. It is addressed above.

  4. It is addressed above.

like image 30
MariaG Avatar answered Sep 22 '22 15:09

MariaG