Search code examples
iosdebuggingviewdidloadviewdidappear

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();
            });
        })

Solution

  • 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();
        });
     })