Understanding dispatch_async

Cover Image for Understanding dispatch_async
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Understanding dispatch_async 🔄

In the world of multithreading, one of the most powerful tools at our disposal is dispatch_async. It allows us to perform tasks off the main thread asynchronously, ensuring smoother user experience and optimal performance. 🚀

🔎 Let's break down the code snippet you provided and understand its purpose:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});

💡 The first parameter of this code is dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0). By using this, we are asking the code to perform tasks on a global concurrent queue of a given priority level.

🌟 Advantage of using dispatch_get_global_queue over the main queue:

1️⃣ Concurrency: The main queue executes tasks sequentially, one at a time, and on the main thread. However, using dispatch_get_global_queue allows tasks to run concurrently on different threads, speeding up time-consuming operations such as network requests or data processing.

2️⃣ Preventing UI blocking: By offloading heavy tasks to a background thread, we prevent the main thread (responsible for UI rendering and user interaction) from being blocked. This ensures a smooth and responsive user interface.

3️⃣ Improved performance: Using multiple threads can greatly improve overall system performance, as it takes advantage of available resources and optimizes resource usage.

🤔 Now, let's address the confusion you mentioned. In the provided code, an asynchronous block is dispatched to a global queue. Inside this block, it performs a synchronous network request using dataWithContentsOfURL:. Finally, it calls fetchedData: on the main thread using performSelectorOnMainThread. The purpose of this code seems to be fetching data asynchronously and updating the UI when done.

🔑 However, there is an issue with the code provided. Blocking the global queue by performing a synchronous network request defeats the purpose of using dispatch_async. It's recommended to use asynchronous networking APIs or wrap the synchronous network request in an asynchronous block to avoid blocking the global queue.

🛠 Here's an updated version of the code, addressing this issue using NSURLSession:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:kLatestKivaLoansURL];
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self fetchedData:data];
            });
        } else {
            NSLog(@"%@", error);
            // Handle error gracefully
        }
    }];
    
    [dataTask resume];
});

✅ In this updated code, we use NSURLSession and its asynchronous dataTaskWithURL method. Once the data is received, we dispatch the UI update to the main queue using dispatch_async(dispatch_get_main_queue()), ensuring it's performed on the main thread.

🤩 Now that you have a better understanding of dispatch_async and its benefits, go forth and conquer the world of multithreading! If you have any more questions or want to share your experiences, leave a comment below. We'd love to hear from you! 🎉


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello