Loading screen IOS

David11011
David11011 used Ask the Experts™
on
I've created an IOS app that has two view controllers. The first view controller loads fairly quickly when you first open the app but when I push to the second view controller it takes around ten seconds to load.

As it is right now, once I push the button to switch to the next view controller, the app appears to freeze and the button remains highlighted allowing no user interaction with the app. I would like to have an animated loading screen displayed while the view controller is loading so that the user doesn't think the app is freezing up.

I've looked online for ways to preload the VC so I can display the loading screen in VC1 while VC2 is loading and then push to VC2 but haven't found anything like that.

Right now all of the audio files are what is causing most of the load time. I have all of the audio files being loaded in the viewDidLoad. I've looked for a way to display something in the view controller before the viewDidLoad but haven't found anything.

Any help you can give would be appricieated. Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
/bump
Commented:
If you need to show the view controller very fast, remove/re-arrange all heavy code from the init methods, from the viewDidLoad. If you perform a long operation in the main queue, the GUI will not respond.

You can use dispatch_async, dispatch_after to perform a long operation. For example:

__block typeof (self) myself = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [myself loadSounds];
});

Open in new window


If it's all about AVPlayer, you can preload the music
NSData *audioData = [NSData dataWithContentsOfURL:musicURL];

Open in new window


When the operation finished, you can update the GUI.

dispatch_async(dispatch_get_main_queue(), ^{ 
            [[self myControl] setNeedsDisplay];      
 });

Open in new window


And later play it:

NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];
NSError *error = nil;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error:&error];
if ( error == nil ) {
            [player setDelegate:self];
            [player play];
}

Open in new window

Author

Commented:
Thanks for the very complete answer!

Commented:
You are welcome

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial