我在修改線程內的視圖時遇到問題。我試圖添加一個子視圖,但它需要大約6秒或更長時間來顯示。我終於搞定了,但我不知道具體如何。所以我想知道爲什麼它的工作,什麼是以下方法之間的區別:主隊列上的performSelectorOnMainThread和dispatch_async有什麼區別?
//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];
僅供參考這個被稱爲類的ACAccountStore的這個Completetion處理程序中。
accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
//my methods were here
}
}
編輯:當我說它沒有工作,我的意思是大約需要6秒鐘來顯示我添加的視圖。
當你說'performSelectorOnMainThread:'不起作用時,它是如何失敗的?你收到錯誤信息了嗎?它是一個運行時錯誤的編譯錯誤?如果你沒有得到一個錯誤,你怎麼知道它失敗了? – 2012-02-17 21:52:09
是'addSubview:'您使用觸摸的UI元素的唯一方法,還是其他人? – 2012-02-17 21:52:38
@AndrewMadsen我忘了提及它的工作原理,但大約需要6秒或更長的時間來顯示。 – 2012-02-17 22:16:34