0

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();
            });
        })
Cœur
  • 37,241
  • 25
  • 195
  • 267
mcfly soft
  • 11,289
  • 26
  • 98
  • 202
  • are you printing logs or checking this by keeping breakpoint in the method? Because if you are running in `release` mode then breakpoints may not pause the program counter as expected. – Gandalf Oct 29 '15 at 15:00
  • @Gandalf. I guess the problem is the debug mode as you suspected. I am developing now 1 year with xcode, but never had such a problem. When halting in the method of didappear and continuing, it failed. But it is not stopped in any of my breakpoints and I couldn't see where it stopped. Very strange. At least I am happy that the problem is not the code. I hope I will remember when I get into the same problem again. Thanks a lot. – mcfly soft Oct 29 '15 at 16:19
  • Oh no. I just restart the app again without debug mode and it hangs again. Started the app several times without debug mode and went through. Didn't change anything, because I am just testing. I really do not understand. – mcfly soft Oct 29 '15 at 17:13
  • Could it be that the following is the problem ? I updated my question. – mcfly soft Oct 29 '15 at 17:18
  • UI presentation and update should be done on main thread. Check this [http://stackoverflow.com/questions/20590802/objective-c-trouble-updating-ui-on-main-thread](http://stackoverflow.com/questions/20590802/objective-c-trouble-updating-ui-on-main-thread) for how to do it. – Gandalf Oct 29 '15 at 17:23
  • I guess updating is not the problem. I am doing this in my dosomething_long(); . But my long runner should run in background, should it not ? If yes, could the problem be, that I am open my subview from my backgroundtask ? – mcfly soft Oct 29 '15 at 17:31
  • 1
    @Gandalf. Sorry was missunderstanding from my side. You are right. I just have to call the subview in the main thread. I will update my question. So problem is solved. Thank you very much for your help. If you like you can answer that I should call the subview in the main thread and I will accept. – mcfly soft Oct 29 '15 at 17:55

2 Answers2

0

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

Community
  • 1
  • 1
jazz
  • 232
  • 1
  • 2
  • 10
0

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();
    });
 })
Gandalf
  • 2,399
  • 2
  • 15
  • 19