Holly
Holly

Reputation: 1976

How to keep a view controller alive for an asynchronous task?

I'm making my first iphone app and had a bit of a mis-understanding.

I had a menu view controller and a game view controller. I wanted my menu view controller to asychronously fetch some weather data from a web service that could be passed onto my game view controller.

In the seque between the menu to the game my thinking was such:

if user presses playgame BEFORE weather data loaded
    store a reference to the game view Controller and send it the data later.
else
    we already have the weather data so we'll just set send that now.

However I didn't realise that once the player transitions to the game view controller the functions that recieve weather data in the menu view controller would stop firing!

Is there a way I can keep one view controller 'alive' and functioning in the background whilst the player is in another view controller?

Upvotes: 0

Views: 285

Answers (1)

Derick F
Derick F

Reputation: 2769

so assuming you're using an nsurlconnection to fetch the data, you could just set the connection's delegate to the game view controller and have that process the data for you.

the more ideal way to do it is to seperate out the weather stuff into a different class that extends nsobject and implements the nsurlconnectiondelegate protocol and just pass along that class as the data to your game view controller since that same class could in theory contain all the weather data as well.

Upvotes: 2

Related Questions