我在修改线程内的视图时遇到问题.我试图添加一个子视图,但显示大约需要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) 有没有办法可以执行一个块而不是一个对应于这个和类似方法的选择器?
我有观察者可能会收到主线程上没有生成的事件.我想要在主线程上执行操作,如果它主要是面向UI的.现在,我需要编写两个方法来执行此操作,其中一个是事件观察者,第二个是需要在主线程上执行的代码.
如果可以的话,我想将这一切都封装到一个方法中.
concurrency multithreading objective-c event-handling objective-c-blocks