(¿Multiproceso?) Combinado con Pushing View Controller

I am creating a game for iphone that has a few hundred levels. These are all loaded into a scroll view when the user presses the start button so that they can choose a level. However, I need to do a lot of calculations, load each level's data from a keyed encoder, and draw a UIImageView on the scrollview before I can show the screen to the user. As it is right now, this is all done by methods I call from viewDidLoad, so it all happens BEFORE the view controller gets pushed onto the stack. As a result, there is a noticeable delay when the user pushes this button.

I would like to have the view controller be pushes immediately, and then have the levels lay themselves out AFTER the view controller is presented, hopefully providing a better user experience. I imagine this would be possible by calling the methods from ViewDidAppear but I would rather start doing the calculations immediately in order to make things as fast as possible.

Is there a way to do this? Possibly using grand central dispatch or assigning the work to another thread?

Gracias por cualquier ayuda!

preguntado el 29 de julio de 12 a las 07:07

2 Respuestas

If you want the UI content to load first (but not necessarily display) before starting your complex calculations, keep your code in the -viewDidLoad method, but insert it after the [super viewDidLoad] call. If the calculations do not involve objects that must be manipulated only on the main thread (i.e. the UI), use Grand Central Dispatch to set up an asynchronous queue for the calculations to be performed in using dispatch_async().

EDIT: I now understand you're using a UINavigationController to push this view onto the stack and that the calculations all have a direct impact on the UI, meaning that they must take place on the main thread. I would thus recommend placing the code in the -viewWillAppear: method. This way, the system should push the view before the calculations are invoked.

Opcionalmente, en el viewDidLoad: method, you could call another method that performs only the calculations and stores the results in variables on an asynchronous thread. The app could then read these calculations in viewDidLoad:.

Respondido 29 Jul 12, 08:07

Thanks for the answer, but I dont think thats quite what I'm looking for. [super viewDidLoad] was already the first line in my viewDidLoad method, and most of the work to be done is either directly on UIButtons or else needs to be done first. Any other ideas? - laboratoriosbkbeach

Really what I want to know is how can I start the calculations immediately AND push the vc immediately, while still waiting unti lthe code is done before updating the UI - laboratoriosbkbeach

Unfortunately that doesnt work either. It doesnt improve performance at all, and more importantly, it is called again when I return to the view, so i end up adding another whole set of every view element - laboratoriosbkbeach

There is no magic method to use in the delegate. You are asking a super general question: How to do threading in iOS.

Respondido 29 Jul 12, 12:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.