iOS: Simple method for running tasks on a background thread (Grand Central Dispatch)

This might come in handy if you're loading large amounts of data into a UITableView, and want your users to have the freedom to browse other parts of your app without waiting for the table to load. One way to do this is by using Grand Central Dispatch (iOS 4 and later):

//Start an activity indicator here

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    //Call your function or whatever work that needs to be done
    //Code in this part is run on a background thread

    dispatch_async(dispatch_get_main_queue(), ^(void) {

        //Stop your activity indicator or anything else with the GUI
        //Code here is run on the main thread

No registration required for commenting, just your name and email address (hidden from public view).
comments powered by Disqus