简体   繁体   中英

strange behaviours viewDidAppear not called

I call a function to open a subview from my parent view.

I see that viewDidLoad is called. In my viewDidLoad is no code. The strange behaviour is, that sometimes viewDidAppear: is not called even I have not change any code.

Of course there can be a lot of reasons, but I do not know how to debug. I am looking for a way to find out where the process hangs at the time when viewDidLoad is finished.

Does anyone have a hint for me or does anyone know what could be the problem? I mean sometimes it works sometime not. Is viewDidAppear: depending on the parentsview or is there something wrong in the subview?

func showSubview() {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("Subview") as! SubViewController
    self.presentViewController(vc, animated: true, completion: nil)
    //self.showViewController(vc, sender: self);
}

UPDATE I am calling the showSubview from a async task , I guess this is not good. What do you think and how shall I do it better ? Is there any complition handler with dispatch_async ?

        dispatch_async(backgroundQueue, {
            dosomething_long();
            showSubview();
        })

UPDATE 2 The problem is, that I am opening the subview from a background task. I should not do this. But the question is how can I call the subview when the background task is finished. Is there any completion handler for dispatch_async call ?

SOLVED WITH HELP OF GANDALF

I am stupid :-) I should call the subview in the main thread:

        dispatch_async(backgroundQueue, {
            dosomething_long();
            dispatch_async(dispatch_get_main_queue(), {
                showsubview();
            });
        })

The question is; is the view created before? Is it caused when you reopen your app from background? You can check the lifecycle from here and I think your problem is occurred because of that. iOS 7 - Difference between viewDidLoad and viewDidAppear

The very obvious reason for why program counter may not go inside methods is that app could be running in the release mode.

Now as we can see after updated question that this is not the reason and there is a UI operation happening at background queue .
As per iOS development guidelines all UI operation should happen on main thread , you should try executing that on main thread instead of a background thread .
Try the below code:

dispatch_async(backgroundQueue, {
    dosomething_long();
    dispatch_async(dispatch_get_main_queue(), {     
        showsubview();
    });
 })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM