相关疑难解决方法(0)

主队列上的performSelectorOnMainThread和dispatch_async有什么区别?

我在修改线程内的视图时遇到问题.我试图添加一个子视图,但显示大约需要6秒或更长时间.我终于搞定了,但我不知道究竟是怎么回事.所以我想知道为什么它有效,以下方法之间有什么区别:

//this worked -added the view instantly
dispatch_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];
Run Code Online (Sandbox Code Playgroud)

作为参考,这是在ACAccountStore类的Completetion Handler中调用的.

accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
            if(granted) {
            //my methods were …
Run Code Online (Sandbox Code Playgroud)

multithreading objective-c uikit grand-central-dispatch ios

53
推荐指数
1
解决办法
2万
查看次数