runloops 和 application:didFinishLaunch 消息顺序
我对 Runloops 的理解很基础,所以这可能看起来是一个非常陈词滥调的问题。
我的 application:didFinishLaunchingWithOptions
(或 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];
}
这些方法中的每一个都按列出的顺序执行吗?或者是否有可能在之前执行步骤 #5应用程序的主运行循环使用 '[window makeKeyAndVisible]'
完成放置主窗口的工作
doSomeTaskHere
是否需要包装到 performSelectorOnMainThread:withObject 中:waitUntilDone:YES
确保运行循环完成窗口的显示,从而在调用“doSomeTaskHere
”之前加载最顶层视图的任何视图?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些任务将在主线程的运行循环上按顺序执行。由于 UI 更新也发生在主线程上,因此在从
-application:didFinishLaunchingWithOptions:
返回之前,您将不允许应用程序重绘屏幕,所以 while[window makeKeyAndVisible];
将在[myTaskObject doSomeTaskHere] 之前完成;
在doSomeTaskHere
完成之前,您仍然阻止 UI 更新。如果 doSomeTaskHere 是一项昂贵的操作,您应该将其安排在运行循环的未来迭代中,或者更好的是在不同的线程上执行该操作,以便 UI 可以更新并响应触摸。
performSelectorOnMainThread:withObject:waitUntilDone:YES
不允许主线程更新 UI,除非您传递NO
作为最后一个参数。告诉主线程等待直到主线程完成其他一些工作并不是很有用,或者与直接调用该选择器有什么不同。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 thatdoSomeTaskHere
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 passedNO
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.