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
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
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
There is no magic method to use in the delegate. You are asking a super general question: How to do threading in iOS.