为了账号安全,请及时绑定邮箱和手机立即绑定

主队列上的performSelectorOnMainThread和dispatch_async

主队列上的performSelectorOnMainThread和dispatch_async

白衣非少年 2019-12-26 14:06:19
我在修改线程内的视图时遇到问题。我试图添加一个子视图,但是显示大约需要6秒钟或更长时间。我终于使它工作了,但我不知道它到底是怎么做到的。所以我想知道为什么它起作用以及以下方法之间的区别是什么://this worked -added the view instantlydispatch_async(dispatch_get_main_queue(), ^{    //some UI methods ej    [view addSubview: otherView];}//this took around 6 or more seconds to display[viewController performSelectorOnMainThread:@selector(methodThatAddsSubview:) withObject:otherView waitUntilDone:NO];//Also didnt work: NSNotification methods -  took also around 6 seconds to display//the observer was in the viewController I wanted to modify//paired to a method to add a subview.[[NSNotificationCenter defaultCenter] postNotificationName: @"notification-identifier" object:object];作为参考,在ACAccountStore类的完成处理程序内部调用了该方法。accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {            if(granted) {            //my methods were here            }}编辑:当我说它不起作用时,我的意思是花了大约6秒钟才能显示我添加的视图。
查看完整描述

3 回答

?
RISEBY

TA贡献1856条经验 获得超5个赞

默认情况下,-performSelectorOnMainThread:withObject:waitUntilDone:仅计划选择器以默认运行循环模式运行。如果运行循环处于另一种模式(例如跟踪模式),则在运行循环切换回默认模式之前它不会运行。您可以使用该变体来解决此问题-performSelectorOnMainThread:withObject:waitUntilDone:modes:(通过传递您希望它在其中运行的所有模式)。

另一方面,dispatch_async(dispatch_get_main_queue(), ^{ ... })一旦主运行循环将控制流返回到事件循环,就会运行该块。它不在乎模式。因此,如果您也不想关心任何模式,dispatch_async()可能是更好的选择。


查看完整回答
反对 回复 2019-12-26
?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

可能是因为performSelectorOnMainThread:withObject:waitUntilDone:使用常见的运行循环模式将消息排队。根据Apple的《并发编程指南》,主队列将把排队的任务与应用程序运行循环中的其他事件进行交错。因此,如果事件队列中还有其他事件要处理,则调度队列中的已排队块可以先运行,即使它们是稍后提交的。


本文是performSelectorOnMainThreadvs. 的出色解释dispatch_async,它也回答了上述问题。


查看完整回答
反对 回复 2019-12-26
?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

你试试PerformSelectorOnMainThread用waitUntilDone=YES


例如:


码:


[viewController performSelectorOnMainThread:@selector(methodThatAddsSubview:) withObject:otherView waitUntilDone:YES];

我认为这可能会解决问题,因为为什么PerformSelectorOnMainThread需要这么长时间才能做出回应。


查看完整回答
反对 回复 2019-12-26
  • 3 回答
  • 0 关注
  • 863 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信