問題描述
我已經閱讀了很多關于是否應該在主線程之外調用 URLForUbiquityContainerIdentifier:
的相互矛盾的信息.在許多 Apple 的文檔中,他們總是大概在主線程上調用此方法.但是,我還了解到調用此方法可能會阻塞很長時間.
I've read a lot of conflicting information about whether or not URLForUbiquityContainerIdentifier:
should be called outside the main thread or not. In a lot of Apple's documentation they always call this method presumably on the main thread. However, I've also read that it's possible that calling this method could block for a significant time.
大家的想法是什么?在主線程中調用它,不用擔心,或者是的,總是在另一個線程中調用它?
What is everyone's thoughts? Call it in the main thread and don't worry or yes, ALWAYS make this call in another thread?
推薦答案
NSFileManager 可能會阻塞,建議在與主線程不同的線程上運行.這是使用 Grand Central Dispatch 在不同線程上利用 iCloud Storage 的片段
NSFileManager can be blocking and is recommended to run on a different thread than the main thread. Here is a snippet of using Grand Central Dispatch to utilize iCloud Storage on a different thread
dispatch_queue_t globalQueue = dispatch_get_global_queue(QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(globalQueue, ^{
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *ubiquityContainer = [fileManager URLForUbiquityContainerIdentifier:nil];
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
[self updateWithUbiquityContainer:ubiquityContainer];
});
});
這是來自這里的一篇很棒的文章:
This is from a great article located here:
http://oleb.net/blog/2011/11/ios5-tech-talk-michael-jurewitz-on-icloud-storage/
這篇關于URLForUbiquityContainerIdentifier: 應該在主線程之外的線程中調用嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!