Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a process in background thread iOS

I want to run a task after 6sec in background in a separate thread. I used this code for that.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 6 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            [self getUnsyncNamesFromServer];
}

I am not sure this run in a background thread. Do i need to use dispatch_async for this purpose. What is the best approach for this kind of situation.

like image 615
Susitha Avatar asked Feb 03 '26 05:02

Susitha


1 Answers

dispatch_async is what you want. In the code you've used, the method inside the block will be after 6 seconds on the main queue.

For the background queue, use the follow:

__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^{

    [weakSelf getUnsyncNamesFromServer];

});

For further reference, here's the GCD Apple Doc: https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html#//apple_ref/c/func/dispatch_async

like image 119
dezinezync Avatar answered Feb 04 '26 17:02

dezinezync



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!