Susitha
Susitha

Reputation: 3349

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.

Upvotes: 2

Views: 5900

Answers (3)

codester
codester

Reputation: 37189

This line will put your task on main thread not on separate thread dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 6 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{

To put in secondary thread you have to put on global concurrent queue or make your own private queue.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 6 * NSEC_PER_SEC),dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Put on global queue to run seprate thread
            [self getUnsyncNamesFromServer];
}

Now to run in background(when application is in background state) you do not need to run on separate thread but if your application is taking too much time to do a task on main thread than you should put on separate thread as it is not advisable to block main thread for too much time.

Your code will not run in background state of application for that you need to register with iOS by calling beginBackgroundTaskWithExpirationHandler:

// Declare property in your class
@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;


-(void)yourfunction{

   self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;

    }];

__weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 6 * NSEC_PER_SEC),dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Put on global queue to run seprate thread
            [weakSelf getUnsyncNamesFromServer];

            if (weakSelf.backgroundTask != UIBackgroundTaskInvalid) {
               [[UIApplication sharedApplication] endBackgroundTask:weakSelf.backgroundTask];
                 weakSelf.backgroundTask = UIBackgroundTaskInvalid;
            }
    });

}

Upvotes: 1

user454322
user454322

Reputation: 7649

Your code would run in the main thread, i.e., not in background, because you are using dispatch_get_main_queue.


Instead of using the main queue, I would create a new one. The code would be something like:

dispatch_queue_t unsyncNamesQueue =
    dispatch_queue_create("UnsyncNamesFromServer", DISPATCH_QUEUE_SERIAL);
//....
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 6 * NSEC_PER_SEC),
               unsyncNamesQueue, ^{
                   [weakSelf getUnsyncNamesFromServer];
               }
);


Be sure to read https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html

Upvotes: 1

dezinezync
dezinezync

Reputation: 3012

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

Upvotes: 7

Related Questions