moving a function to a background thread in objective c
Solution 1
Well that's pretty easy actually with GCD. A typical workflow would be something like this:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
// Perform async operation
// Call your method/function here
// Example:
NSString *result = [anObject calculateSomething];
dispatch_async(dispatch_get_main_queue(), ^{
// Update UI
// Example:
self.myLabel.text = result;
});
});
For more on GCD you can take a look into Apple's documentation here
Solution 2
Also to add, sometimes you don't need to use GCD, this one is very simple to use :
[self performSelectorInBackground:@selector(someMethod:) withObject:nil];
MoKaM
Updated on September 26, 2020Comments
-
MoKaM over 3 years
I have a function that returns a string that takes 15 seconds to compute on an iPhone.
I want to be able to run the function on the background thread so that the main thread can be used for the user interface.
I've heard
GCD
is a new technology that is good for this, can someone provide some example code in regards to how this would work?That is to run a generic function on the background thread and return the result to a UI text field.
EDIT:
Thanks Alladinian it works a treat.
However, when I use GCD my function takes 1 second longer to execute on the iphone simulator (I'd guess this'd be about 5 seconds on an iphone (ill have to test it later today to be sure))
Is there any reason why this is? Perhaps the background thread is slower or something?
-
Jelle De Laender over 11 yearsIf you send a nil as object, why do you define a parameter to your method? @selector(someMethod) is better when you don't use/want to send a parameter
-
Trausti Thor over 11 yearsJust a habit, f.ex. the someMethod might be called with a different action somewhere else, so I usually do - (void)someMethod:(id)sender; it is just a habit of mine, you can just as easily do just someMethod inside the selector, you only need the parameter if you want to check on the sender or if you pass an object
-
Trausti Thor over 11 yearsif you want to f.ex. just run a method that updates the view, you can also do [self performSelectorOnMainThread....
-
MoKaM over 11 yearsThanks Alladinian it works a treat. However, when I use GCD my function takes 1 second longer to execute on the iphone simulator (I'd guess this'd be about 5 seconds on an iphone (ill have to test it later today to be sure)) Is there any reason why this is? Perhaps the background thread is slower or something?
-
Alladinian over 11 yearsYou can try to set the priority to a higher level. Try
DISPATCH_QUEUE_PRIORITY_HIGH
(instead ofDISPATCH_QUEUE_PRIORITY_DEFAULT
). Also do not forget that you get a responsive UI in return, so I guess it's normal to see some performance impact. -
MoKaM over 11 yearstrue. Does GCD work only on dual cores? because iphone 4 isn't dual core as far as i know, so perhaps this wouldn't work on it? maybe i'm wrongly conflating threads/dispatch queues with cores?
-
Alladinian over 11 yearsGCD should work just fine on any Apple device with iOS4+ (to be honest I'm not really so deep into this stuff either. Maybe someone here can shed some light?)
-
kgreenek about 10 yearsSpeaking at a general CS level, using a background thread on a single core is still better. To massively over-simplify, imagine the CPU executing 1 operation from your background thread for every 10 operations in the main thread as an example.
-
Glenn Posadas about 3 yearsThanks for this. This is a nice working sample code-block for dispatching a task into a bg thread. My GCD knowledge is not really strong, and when I attempted to put my time-consuming for-loop that extracts data and sets up a dictionary, I get a crash that says
pointer being freed was not allocated
. After using this sample, it's now working fine.