How can I verify that I am running on a given GCD queue without using dispatch_get_current_queue()?

Assign whatever identifier you want using dispatch_queue_set_specific(). You can then check your identifier using dispatch_get_specific(). Remember that dispatch_get_specific() is nice because it’ll start at the current queue, and then walk up the target queues if the key isn’t set on the current one. This usually doesn’t matter, but can be useful in some cases.

How to asynchronous load image from a web-server in UICollectionView using NSCache

Try this one it’s Working code (Swift 4). func NKPlaceholderImage(image:UIImage?, imageView:UIImageView?,imgUrl:String,compate:@escaping (UIImage?) -> Void){ if image != nil && imageView != nil { imageView!.image = image! } var urlcatch = imgUrl.replacingOccurrences(of: “https://stackoverflow.com/”, with: “#”) let documentpath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] urlcatch = documentpath + “https://stackoverflow.com/” + “\(urlcatch)” let image = UIImage(contentsOfFile:urlcatch) if image != nil … Read more

How can I retrieve a return value from a completion block?

You’re missing some basics about asynchronous development with blocks. You can’t have a dispatched block return from anywhere but its own scope. Think of each block as its own method, instead of inline code. I think what you’re looking for is something similar to this… – (void)testWithHandler:(void(^)(int result))handler { [obj somemethodwithcompeltionblock:^{ int someInt = 10; … Read more

Waiting for multiple asynchronous download tasks

In Swift 4, func executeMultiTask() { //1. Create group let taskGroup = DispatchGroup() //2. Enter group taskGroup.enter() myTask1.execute(completeHandler: { // … //3. Leave group taskGroup.leave() //< balance with taskGroup.enter() }) /* Add more tasks … //2. Enter group taskGroup.enter() myTask2.execute(completeHandler: { //3. Leave group defer { // Use `defer` to make sure, `leave()` calls are … Read more

How to dispatch on main queue synchronously without a deadlock?

I need to use something like this fairly regularly within my Mac and iOS applications, so I use the following helper function (originally described in this answer): void runOnMainQueueWithoutDeadlocking(void (^block)(void)) { if ([NSThread isMainThread]) { block(); } else { dispatch_sync(dispatch_get_main_queue(), block); } } which you call via runOnMainQueueWithoutDeadlocking(^{ //Do stuff }); This is pretty much … Read more

Core Data and threads / Grand Central Dispatch

Here’s a good example for you to try. Feel free to come back if you have any questions: self.mainThreadContext… // This is a reference to your main thread context NSPersistentStoreCoordinator *mainThreadContextStoreCoordinator = [self.mainThreadContext persistentStoreCoordinator]; dispatch_queue_t request_queue = dispatch_queue_create(“com.yourapp.DescriptionOfMethod”, NULL); dispatch_async(request_queue, ^{ // Create a new managed object context // Set its persistent store coordinator NSManagedObjectContext … Read more