I have an asynchronous dispatch queue
which reads data from large files in the background. During the course of that it does a few other things including some NSTask
operations. The problem I'm facing is that I populate some variables with the result of those operations and the background queue has already moved on by the time those variables are ready (not NULL).
My code looks similar to the following:
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(backgroundQueue, ^{
// reading large files
...
NSTaskClass *operation = [[NSTaskClass alloc] init];
NSString *result = [operation doTask:filePath];
NSLog(@"result: %@" result); // returns NULL since task isn't done yet.
...
// continue large file operations
});
Would would be the best way to handle this? I looked into creating a callback
but I couldn't figure it out, and I'm not sure if that's even the right approach. Any advice on best practices is appreciated, thanks.