2
正如bbumhere, 指出的那樣,文檔中提到:「大多數情況下,UIKit類只能從應用程序的主線程中使用。對於派生類UIResponder尤其如此,或者涉及操縱用戶界面你的申請以任何方式。「」。使用GCD在後臺創建UIKit對象是一種不好的做法嗎?
我以爲我明白,繪圖方法不能在後臺線程中調用,因此創建可以在後臺完成,因爲只有在添加視圖時才調用drawRect方法。但也許我錯了。
總之,這樣的代碼有風險嗎?
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSString *fileName = [pathToModel stringByAppendingPathComponent:[[compDico valueForKey:@"fileName"] lastPathComponent]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:
fileName]];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 62, 190, 20)];
[label setText:[[someArray objectAtIndex:i-1] someText]];
[label setNumberOfLines:0];
label.font=[UIFont fontWithName:@"arial" size:10.0f];
[label setBackgroundColor:[UIColor clearColor]];
// Create some other view here
// ...
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imageView];
[self.view addSubview:label];
//Add other view here
// ...
});
});
非常感謝您的回覆!
這裏有一些很好的信息:http://stackoverflow.com/questions/16299842/uikit-and-gcd-thread-safety?rq=1 – rmaddy
如果它被記錄爲不支持,比它不支持。 *巧合工作*不是一種可接受的併發方法。它可能在你測試過的情況下「起作用」,但在下一次軟件更新後失敗,在沒有的硬件上失敗,或者在某些你從未測試過的客戶配置上失敗。 (感謝您提出一個明確的問題,順便說一句) – bbum
關於OP的問題,單獨的和稍微不相關的問題。 @bbum會知道答案,很抱歉在這裏發佈,但不覺得它應該有自己的問題。 '0ul'在創建調度隊列中意味着什麼? –