Question

This code, while working, executes the two tasks in FIFO order:

-(void) update {

    @autoreleasepool {

        dispatch_queue_t queue = dispatch_queue_create("us.yellosoft", 0);

        // inpternal address
        dispatch_async(queue,^(){

            // TODO: implement NSProgressIndicator logic

            [internalIpMenuItem setTitle: @"Updating..."];

            // get IP address from [[NSHost currentHost] addresses]...
            NSString *localIP = [self getLocalIP];

            // change UI
            [internalIpMenuItem setTitle: localIP];

        });

        // external address
        dispatch_async(queue, ^(){

            // TODO: implement NSProgressIndicator logic

            [externalIpMenuItem setTitle: @"Updating..."];

            // get IP address from external JSON service...
            NSString *externalIP = [AddressService getIPaddress];

            // change UI
            [externalIpMenuItem setTitle: localIP];

        });

    }

}

I would like to have two tasks run concurrently. Is this possible?

Was it helpful?

Solution

You "can", but you need to dispatch to the main thread if you use UIKit methods anyway.

In order to parallelize work, you should use the global concurrent queue. Using your own concurrent dispatch queue, realy only makes sense if you want to synchronize access to shared resources - in which case you would need to use dispatch_barrier_async and dispatch_barrier_sync.

OTHER TIPS

From the docs:

In iOS 5 and later, you can create concurrent dispatch queues yourself by specifying DISPATCH_QUEUE_CONCURRENT as the queue type. In addition, there are four predefined global concurrent queues for your application to use. For more information on how to get the global concurrent queues, see “Getting the Global Concurrent Dispatch Queues.”

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top