James
James

Reputation: 201

Would I use dispatch in block for MBProgressHud?

In the MBProgressHud documentation it states to use this inside of a dispatch like so:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    // Do something...
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
});

Which is completely understandable considering you don't want it to boggle up the main thread. But could I just do this instead when using a block:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error)
                {

                }
                else
                {
                   [MBProgressHUD hideHUDForView:self.view animated:YES];
                }
            }];

Or would I still have to use dispatch?

Upvotes: 2

Views: 2439

Answers (1)

Amir
Amir

Reputation: 9637

Change your code to be like this:

 [MBProgressHUD showHUDAddedTo:self.view animated:YES];
 [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
         NSLog(@"Am I running on the main thread: %@", [NSThread isMainThread] ? @"YES": @"NO");
         if (error)
         {

         }
         else
         {

         }
   }];

if it logs "YES" then you don't need to run [MBProgressHUD hideHUDForView:self.view animated:YES]; on the main thread, otherwise you need to use

dispatch_async(dispatch_get_main_queue(), ^{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
});

Update:

blocks are run on whatever thread they've been called from, notice following example:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self longRunningProcessWithCompletionBlock:^{
        NSLog(@"Is running on the main thread? %@", [NSThread isMainThread] ? @"YES" : @"NO");
    }];
}



- (void)longRunningProcessWithCompletionBlock:(void (^)(void))completionBlock {

    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    //this is running on the concurrent thread, so does completionBlock() as it has been called on a concurrent thread.
    dispatch_async(concurrentQueue, ^{
        [NSThread sleepForTimeInterval:3];
        completionBlock();
    });
}

So Basically the result of above will be "Is running on the main thread? NO"

Again I have exact same call on viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self longRunningProcessWithCompletionBlock:^{
        NSLog(@"Is running on the main thread? %@", [NSThread isMainThread] ? @"YES" : @"NO");
    }];
}

But this time, I'm calling completionBlock of longRunningProcessWithCompletionBlock on the main thread as follow:

- (void)longRunningProcessWithCompletionBlock:(void (^)(void))completionBlock {

    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(concurrentQueue, ^{

        [NSThread sleepForTimeInterval:3];

        //notice the difference, this time we are calling the completionBlock on the main thread, so that block will be running on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            completionBlock();
        });

    });
}

This time because we have called the completion block on the main thread, the result will be Is running on the main thread? YES

So in a nutshell, block does not guarantee that they are getting executed on the main thread! but they can guarantee that they will be executed on whatever thread they've been called from.

In your case Parse.com developers are calling the completion handler block of deleteInBackgroundWithBlock on the main thread and that's why you saw that log was "yes".So you just need to call [MBProgressHUD hideHUDForView:self.view animated:YES]; without dispatch_async(dispatch_get_main_queue(), ^{ }); (as it is already on the main thread and this is an extra unnecessary step)

Upvotes: 2

Related Questions