Difference requiresMainQueueSetup and dispatch_get_main_queue?

馋奶兔 提交于 2019-12-05 16:33:53

问题


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?


回答1:


  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.



来源:https://stackoverflow.com/questions/50773748/difference-requiresmainqueuesetup-and-dispatch-get-main-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!