开发者

Why is there a delay when presenting this modal view?

开发者 https://www.devze.com 2023-02-09 20:32 出处:网络
I have an app that starts with a tableview (from a xib) that loads multiple navigation controllers.I want to present a modal view on startup if a long init sequence is underway.I tried presenting a mo

I have an app that starts with a tableview (from a xib) that loads multiple navigation controllers. I want to present a modal view on startup if a long init sequence is underway. I tried presenting a modal view in the App Delegate, but the view doesn't appear until well after the underlying code is already complete.

MainWindow.xib loads TableViewController, so I put my call to presentmodalview in that View Will Appear with the same result. I have numerous NSLOG calls so I can watch what is happening but I can't figure out why the view doesn't appear until after both the app delegate and the tableview controller's viewWillAppear finish. I moved the call to viewDidAppear with the same result. Here is a code snippet:

App Delegate:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // Add the tab bar controller's current view as a subview of the window
    [window addSubview:tabBarController.view];
    [window makeKeyAndVis开发者_JAVA技巧ible];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];

    // if new version being installed do init stuff
    if ( <needs update code here>) {
        Uncompress *uncompressView = [[Uncompress alloc] initWithNibName:@"Uncompress" bundle:nil];
        [self.tabBarController presentModalViewController:uncompressView animated:NO];
        [uncompressView release];
    }
}

I also tried changing presentmodalviewcontroller to [window addSubview:uncompressView.view] without any luck.

The Update code runs just fine, the problem is the view doesn't appear until both the AppDelegate and the TableView are finished. I'm not creating any views programatically - all are from Xib's. I can't figure out where to call the update function to get the view to appear right away. Any help appreciated. Thank you!


On iOS, the UI is only updated when your code returns control to the run loop. So if your uncompress task takes a lot of time, the UI will only be updated after it has finished.

You can sort of work around this issue by putting the time-intensive task in a separate method and calling that method with [self performSelector:... afterDelay:0.0 ...]; but it is not a good solution because even if the UI will update, user interaction with your app will still be blocked as long as your code blocks the main thread. And if your code takes more than a few seconds to run (e.g. because it runs on an older, slower device), the OS watchdog timer will kill your app.

The best solution would be to put the time-intensive task in a background thread, e.g. with NSOperation or dispatch_async().

0

精彩评论

暂无评论...
验证码 换一张
取 消