Xcode version: 7.2.1, iOS version: 9.1+
I am trying to display a UIAlertController alert message on the iPad that shows a "Loading...Please Wait" message without any buttons on it. I present the UIAlertController before starting a long operation, and then after the long operation, I dismiss the UIAlertController. However, what is happening is the UIAlertController DOES NOT show up immediately. It only flashes up briefly AFTER the longer operation is completed, and then it is dismissed.
Following is the general code structure:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Loading" message:@"Please wait...\n\n\n" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
/// Perform long operation here...
/// (Basically, a message is being sent to a server, a long operation happens on
/// the server, and then the server returns a response to the iPad client.)
[alert dismissViewControllerAnimated:YES completion:nil];
}
I have tried a number of alternatives, like using dispatch_async so that the alert message can be displayed simultaneously while the long operation is occurring:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Loading Dataset" message:@"Please wait...\n\n\n" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/// Perform long operation here...
dispatch_async(dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
});
}
I've tried using UIAlertView (which I know is deprecated in iOS 8), both alone and with dispatch, but this is not working either.
I've tried wrapping the long operation code in a performSelector message, but to no avail.
In the past, using a UIAlertView with a dispatch_queue has worked seamlessly for this situation.
Any assistance is appreciated.
Edit:
One interesting thing to note: In the dispatch_async code, if I add a usleep(250000) right before calling the long operation code, about 80% of the time, the UIAlertController alert message WILL be displayed at the correct time: BEFORE the long operation begins. However, this is not 100% of the time, and is not a sustainable solution. Calling usleep with a smaller number does not work.
Note that for the DISPATCH_QUEUE_PRIORITY_DEFAULT option, I also tried: DISPATCH_QUEUE_PRIORITY_HIGH, DISPATCH_QUEUE_PRIORITY_LOW, and DISPATCH_QUEUE_PRIORITY_BACKGROUND
And, I tried the following too:
dispatch_queue_t myQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
dispatch_async(myQueue, ^{
/// Perform long operation here...
dispatch_async(dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
});