If I only use dispatch_sync it does not achieve the result I want, the function returns before the block is executed.
I think this happens because this function is called from a thread that´s not the main one.
Here´s the code below, when I put breakpoints, it executes in the following order:
1. Block (presentViewController)
2. function returns with result = NO
3. Alert appears on screen.
4. User presses Yes/No
5. Executes AlertAction
At this point, since the function has already returned, it does not achieve the expected result.
__block BOOL result = NO;
// dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_sync(dispatch_get_main_queue(), ^{
UIViewController *rootController = [IOSUtil visibleViewController];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[self getResourceText:caption]
message:[self getResourceText:message]
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:cancelButtonText
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
[LayoutIOS internalShowProgressControl];
// dispatch_semaphore_signal(semaphore);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:okButtonText
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
result = YES;
[LayoutIOS internalShowProgressControl];
// dispatch_semaphore_signal(semaphore);
}]];
[LayoutIOS internalHideProgressControl];
[rootController presentViewController:alertController animated:YES completion:nil];
});
// dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return result;