Block version of performSelectorOnMainThread: withObject: waitUntilDone:

Is there a way I can execute a block and not a selector that matches this and similar methods?

I have observers that can receive events that are not generated in the main thread. I want the action to be executed in the main thread if it focuses mainly on the user interface. Right now, I need to write two methods for this, where one is an observer of events, and the second is code that should be executed in the main thread.

I would like to encapsulate it all in one method, if I could.

+6
source share
2 answers

The preferred technology for block support glut operations is called Grand Central Dispatch. You can find sample code on Wikipedia and in the link to the Central Dispatch Service (GCD)

dispatch_async(backgroundQueue, ^{ //background tasks dispatch_async(dispatch_get_main_queue(), ^{ //tasks on main thread }); }); 
+11
source

GCD should do the trick:

 dispatch_sync(dispatch_get_main_queue(), ^{ // Do stuff here }); 

Or dispatch_async if you plan to use waitUntilDone:NO . The main queue is guaranteed to work in the main thread, so it is safe for user interface operations.

+13
source

Source: https://habr.com/ru/post/909005/


All Articles