0

I'm trying to figure out how best to handle the appDelegate methods for multitasking changes and I want to consider any possible behavior by the user.

What happens if you have a lot of ivar initializations and view setups going on in your view controller's method:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

And while this method is half-way through, the user quits your app. But, because it supports multi-tasking, when they open it back up again, is the above init method going to just continue where it left off?

If so, is this the reason why it is better to place initializations in the above method rather than in viewDidLoad since, if you quit the app while viewDidLoad is running, it would seem that viewDidLoad does not pick up where you left off if you then run the app again.

johnbakers
  • 24,158
  • 24
  • 130
  • 258

1 Answers1

1

The method - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil will finish its execution provided that you mean by quit, the fact that the app is going to background.

For example try to add the following to that method and than press the button to put app to background:

    for (int a=0 ; a< 50000; a++) {
      NSLog(@"t");
    }

You should see that the logging is still going on after you send the app to background.

To answer your second question ... You should do initialization in init because there you have the certainty that it will get called only once per object live and its very beginning. Of course if you destroy some objects for example in viewDidUnload you should recreate them in viewDidLoad. SO it everytime depends on how long you want the values of ivars to be around. See for example this SO question: What exactly must I do in viewDidUnload?

Community
  • 1
  • 1
Ondrej Peterka
  • 3,349
  • 4
  • 35
  • 49