Use queue and semaphore for concurrency and property wrapper?

FWIW, there are many synchronization techniques that offer better performance than semaphores. For example, you can use the reader-writer pattern with concurrent queue, where reads are done synchronously, but are allowed to run concurrently with respect to other reads, but writes are done asynchronously, but with a barrier (i.e. not concurrently with respect to any … Read more

Block_release deallocating UI objects on a background thread

You can use the __block storage type qualifier for such a case. __block variables are not automatically retained by the block. So you need to retain the object by yourself: __block UIViewController *viewController = [myViewController retain]; dispatch_async(backgroundQueue, ^{ // Do long-running work here. dispatch_async(dispatch_get_main_queue(), ^{ [viewController updateUIWithResults:results]; [viewController release]; // Ensure it’s released on main … Read more

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on dispatch_semaphore_dispose

From your stack trace, EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) occurred because dispatch_group_t was released while it was still locking (waiting for dispatch_group_leave). According to what you found, this was what happened : dispatch_group_t group was created. group‘s retain count = 1. -[self webservice:onCompletion:] captured the group. group‘s retain count = 2. dispatch_async(…., ^{ dispatch_group_wait(group, …) … }); … Read more

Can you use cancel/isCancelled with GCD/dispatch_async?

GCD does not have built-in support for cancellation; if it’s important to be able to cancel your background task, then checking a flag like you’ve demonstrated is an acceptable solution. However, you may want to evaluate how quickly the cancellation needs to respond; if some of those methods calls are fairly short, you may be … Read more

How to asynchronously load an image in an UIImageView?

The problem is that UIImage doesn’t actually read and decode the image until the first time it’s actually used/drawn. To force this work to happen on a background thread, you have to use/draw the image on the background thread before doing the main thread -setImage:. This worked for me: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ UIImage * img … Read more