Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Ensure execution on main thread [duplicate]

I want to know how to call my function on the main thread.

How do I make sure my function is called on the main thread?

(this follows a previous question of mine).

like image 791
user236739 Avatar asked Jul 20 '12 15:07

user236739


People also ask

How we can ensure execute code in the main thread?

If you're on a background thread and want to execute code on the main thread, you need to call async() again. This time, however, you do it on DispatchQueue. main , which is the main thread, rather than one of the global quality of service queues.

Why do you have to update the UI on the main thread?

It has been in the loop of constantly processing events and hibernation to ensure that user events can be responded as soon as possible. The reason why the screen can be refreshed is because `Main Runloop` is driving. Assume we use our Magical UIKit, and update UI on background threads.

What is main thread in IOS?

The main thread is the one that starts our program, and it's also the one where all our UI work must happen. However, there is also a main queue, and although sometimes we use the terms “main thread” and “main queue” interchangeably, they aren't quite the same thing.


1 Answers

This will do it:

[[NSOperationQueue mainQueue] addOperationWithBlock:^ {     //Your code goes in here    NSLog(@"Main Thread Code");  }]; 

Hope this helps!

like image 94
shoughton123 Avatar answered Oct 11 '22 09:10

shoughton123