Question

My understanding of Runloops is basic so this may seem like a very trite question.

I have the following in my application:didFinishLaunchingWithOptions (or applicationDidFinishLaunching):

{
// 1. typical app setup work: create various views, create a tab bar, add 
// navigation controller and views to the tab bar 

// 2. perform some other app initialization tasks

// 3. Add main view to the window
[window addSubview:tabbarController.view];

// 4. Make window visible
[window makeKeyAndVisible];

// 5. Perform one final behind the scene task
[myTaskObject doSomeTaskHere];
}

Do each of these methods get executed in order listed or is there any chance that step #5 can happen before the app's main runloop completes the work of putting up the main window with '[window makeKeyAndVisible]'

Does the doSomeTaskHere need to get wrapped up into a performSelectorOnMainThread:withObject:waitUntilDone:YES to ensure that the runloop completes the displaying of the window and thus loading whatever view that is the topmost view before 'doSomeTaskHere' is invoked?

Was it helpful?

Solution

Those tasks will execute in order on the main thread's run loop. Since UI updates also occur on the main thread you will not allow your app to redraw the screen until after you return from -application:didFinishLaunchingWithOptions: so while [window makeKeyAndVisible]; will complete before [myTaskObject doSomeTaskHere]; you are still blocking the UI from updating until that doSomeTaskHere is complete.

If doSomeTaskHere is an expensive operation you should schedule it for a future iteration of the run loop or better yet do that work on a different thread so that the UI can update and respond to touches.

performSelectorOnMainThread:withObject:waitUntilDone:YES would not allow the main thread to update the UI as unless you passed NO as the last parameter. Telling the main thread to wait until the main thread finishes some other work isn't very useful or any different than invoking that selector directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top