SUDHAKAR RAYAPUDI
SUDHAKAR RAYAPUDI

Reputation: 551

How to do a Task before launching the app in iOS?

I'm writing an app where I've to load some images from server and then I've to put in my model then start main View controller and then load my images from model to collection View. how to do it ?

Upvotes: 0

Views: 73

Answers (2)

Brian Sachetta
Brian Sachetta

Reputation: 3463

You want to be careful about when and where you perform this logic. Sure, you could theoretically put the fetching code within:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

But I really wouldn't recommend doing so. The first reason why is because you probably want to wait until your images are loaded. You shouldn't perform a network call within the method above because you don't know how long it will take to run. Apple recommends that you return YES from this method as quickly as possible. Not doing so could result in the 0x8badf00d crash - "application failed to launch in time".

What I suggest doing instead, is creating a "loading view controller" that is the app's root view controller after launch. This view controller should kick off the image loading, tell the user what is happening (with a spinner or something like that) and wait until the images are loaded to push your main view controller onto the stack (or present it or whatever). Your images will be hydrated and you can use them to populate your collection view.

Your main view controller should have some kind of reference to these images (either pass them in from the loading view controller or create a data singleton to retrieve them) and use the UICollectionViewDataSource and UICollectionViewDelegate methods to populate the collection view cells (using the images).

Upvotes: 1

Maarten
Maarten

Reputation: 351

The first method where you can start doing work is

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    /* start loading your images here, 
       cache them and let your viewcontroller reload */
    return YES;
}

in AppDelegate.m

Upvotes: 1

Related Questions