Chun
Chun

Reputation: 1988

Decrease loading time between init and viewDidLoad in a UIViewController?

In improving the performance of my app, I'm trying to decrease the amount of time it takes for a UIViewController to load and appear. I've tagged each of my methods to log how long it takes for each to load (this was done on an old iPhone 4 to exaggerate the times).

2013-07-03 15:50:32.546 MyApp[3047:907] init begin
2013-07-03 15:50:32.590 MyApp[3047:907] init end
2013-07-03 15:50:33.085 MyApp[3047:907] viewDidLoad begin
2013-07-03 15:50:33.194 MyApp[3047:907] viewDidLoad end

As you can see, there's about 0.5 seconds between init ends and viewDidLoad begins. To my understanding, this is where loadView occurs, but I'm not really touching that at all.

My question is: what is the best practice for decreasing the amount of loading time here? Is there anything I am able to do programmatically, or is it strictly the limitation of the device?


EDIT:

Some code:

MyViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *myNavController = [[UINavigationController alloc] initWithRootViewController:myViewController];
myNavController.navigationBar.barStyle = UIBarStyleBlack;

[myNavController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:myNavController animated:YES completion:nil];

Some more time logs:

2013-07-03 21:07:26.813 MyApp[3612:907] begin presentViewController:animated:completion
2013-07-03 21:07:27.292 MyApp[3612:907] begin viewDidLoad
2013-07-03 21:07:27.401 MyApp[3612:907] finish viewDidLoad
2013-07-03 21:07:27.444 MyApp[3612:907] finish presentViewController:animated:completion
2013-07-03 21:07:27.508 MyApp[3612:907] begin loading tableView
2013-07-03 21:07:27.760 MyApp[3612:907] finish loading tableView
2013-07-03 21:07:28.513 MyApp[3612:907] viewDidAppear

My view controller is loaded from a NIB and contains a tableView and a UIDatePicker. There seems to be this 0.5 second gap between begin presentViewController:animated:completion and begin viewDidLoad. Since I don't have any code here, I don't know where/how to even approach reducing this time, or if it's even possible.

Upvotes: 1

Views: 902

Answers (3)

Islam Adel
Islam Adel

Reputation: 575

If you are loading images from the internet with large size you can use lazy loading which puts an initial image until the image is loaded and this will help the view appear faster.

Upvotes: 0

ldindu
ldindu

Reputation: 4380

Recommend performing task which takes longer task on background thread but keep in mind UI specific needs to be done on main thread as UIKit components are not thread safe.

For instance, if you are showing image on image view, which are loaded initially from internet, the loading task has to be done on background thread and once the image's data is loaded, render it on main thread.

The methods you have mentioned above are view controller's life cycle which gives you flexibility in defining what you can implement at that very specific situation. For instance, in init, you can initialise all your instance variables, on viewDidLoad, you can setup your components, and there is another method which gets called viewWillAppear, everytime when view appears and disappears, a place for setting and resetting delegates, notifications, observers and others.

And there are other methods such as viewDidAppear, viewDidDisappear, viewDidUnload(deprecated from iOS 6.0) and others to handle view controller's life cycle events, in other states to control and manage view controller's states.

Upvotes: 0

bandejapaisa
bandejapaisa

Reputation: 26952

Logging output has an impact on performance. This is probably a lot quicker without those log output statements.

However, is this really the slowest area of your code when you run it through instruments time profiler? I doubt that.

Profile your code and concentrate on increasing the performance of the biggest bottlenecks first.

Upvotes: 3

Related Questions