Asynchronous method in the background in Objective C

asynchronous, background, objective-c, thrift

Solution

GCD (Grand Central Dispatch) offers easy-to-use functions to execute code asynchronously on a background thread:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [yourInstance syncLocalData];
});

Depending on your needs, you can use the global queue (which is a "concurrent" queue), or create your own "serial" queue.

Problem

I want to be able to send data while not blocking the UI. These files are quite big, so synchronously sending them is not an option. I have a class that implements all the methods of sending data through Apache Thrift. All the asynchronous requests I've seen are using NSURLRequest and NSURLConnection, but for my application I want to utilize my class. Basically, I want to asynchronously call this method: ``` - (void)syncLocalData { Manager *stateManager = [[Manager alloc] init]; [stateManager readDirectory]; } ``` In readDirectory, I go through the device directory and send over data if there is a file.

Original source