前回、Twitterのアカウントの使用許諾を得るために、Twitterの認証を行うというエントリーを書きました。
ACAccountStoreのrequestAccessToAccountsWithTypeメソッドを使って、usernameやらidを取得することができるのですが、そのメソッド内部で、Viewの更新を行うととてつもなく時間がかかることが判明。
具体的には、こんな感じ。
ACAccountStore *aCAccountStore = [[ACAccountStore alloc] init];
ACAccountType *aCAccountType = [aCAccountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[aCAccountStore requestAccessToAccountsWithType:aCAccountType withCompletionHandler:^(BOOL granted, NSError *error) {
if (granted) {
//問題個所(ものすごく時間がかかる)
UIVIew *hoge;
[self.view addSubView:hoge];
}else{
NSLog(@"not grandted");
}
}];どうしてかなーっと思って調べたら、ACAccountStoreのcompletionHandlerはサブスレッドっぽいでサブスレッドであることが反映。従って、gdcを使って、メインスレッドで処理を書けばいいことがわかる。下のような感じ。ACAccountStore *aCAccountStore = [[ACAccountStore alloc] init];
ACAccountType *aCAccountType = [aCAccountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[aCAccountStore requestAccessToAccountsWithType:aCAccountType withCompletionHandler:^(BOOL granted, NSError *error) {
if (granted) {
main_queue = dispatch_get_main_queue();
dispatch_async(main_queue, ^{
UIVIew *hoge;
[self.view addSubView:hoge];
});
}else{
NSLog(@"not grandted");
}
}];実行した結果、無茶苦茶早くなりました。ってか、ACAccountStoreのリファレンスに書いておいてほしかったwww
0 コメント:
コメントを投稿