Search code examples
iosobjective-cobjective-c-blocksdispatchdispatch-async

dispatch_sync block my UI, I cant click any button


I have 2 blocks with dispatch_sync, when the first block ends I show the window for the user and starts run the second block. But I'm not getting click any button on the screen until the second block ends..

Look the code:

[HUD showUIBlockingIndicatorWithText:@"Loading..."];
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^ {
        dispatch_sync(dispatch_get_main_queue(), ^{
                [UIView beginAnimations:@"fade" context:nil];
                [UIView setAnimationDuration:0.5];
                self.capa.alpha = 0.0;
                [UIView commitAnimations];
                [HUD hideUIBlockingIndicator];
        });
});

dispatch_barrier_async(queue, ^ {
    //code executed in the background
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"entrou na foto");
        //[self getFotos];
    });
});

Solution

  • If you call any code on the main thread, it will block your UI, so calling

    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"entrou na foto");
        [self getFotos];
    });
    

    will block your UI until [self getFotos] has returned.