I am posting a notification from the UIPopoverController back to my main view, which then immediately calls dismissPopoverAnimated, and then goes about doing some fairly heavy work loading web views. All of this code works; the problem is that on some older ipads, the popover is not actually dismissed until after the cpu intensive work is completed (verified in debugger). This causes the popover to appear to hang for second after being dismissed. How can I ensure the popover is dismissed immediately instead of doing the intensive code first?
The method which responds to the notification is as follows:
- (void)changeDefaultView:(NSNotification *)note
{
[self closePopover];
int i;
for(i = 0; i < [arrWebViewControllers count]; i++)
{
WebViewController *wvc = [arrWebViewControllers objectAtIndex:i];
[[wvc webview] stopLoading];
[[wvc webview] removeFromSuperview];
[[wvc imageview] removeFromSuperview];
wvc = nil;
}
[arrWebViewControllers removeAllObjects];
[arrLinks removeAllObjects];
[arrImageViews removeAllObjects];
[self loadCategory:[note object]];
[self addWidgetsToView];
}
and closePopover is:
- (void)closePopover
{
if(popover != nil)
{
[popover dismissPopoverAnimated:YES];
popover = nil;
}
}
The animation of the disappearing popover happens on the main run loop; if you’re doing heavy CPU work on the main thread, then the loop won’t get a chance to run. So what you need to do is to perform your work on a background thread.
The currently-preferred way to do this is using Grand Central Dispatch, like so: (I’m assuming that
loadCategory:is the CPU-intensive operation)