1

私はそのような問題に初めて遭遇しました、そしてあなたの助けを必要としています。

Twitterのユーザー情報を取得しています。NSLogを使用するとコンソールで表示できますが、ラベルまたはテキストビューで表示すると、さらに時間がかかり、ほぼ1分かかります。

私が使用しているコードは...です。

_accountStore = [[ACAccountStore alloc] init];
            ACAccountType *accountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

 [_accountStore requestAccessToAccountsWithType:accountType options:nil
                                                completion:^(BOOL granted, NSError *error)

             // Request access from the user to use their Twitter accounts.

             {
                 // Did user allow us access?
                 if (granted == YES)
                 {
                     // Populate array with all available Twitter accounts
                     NSArray *arrayOfAccounts = [_accountStore accountsWithAccountType:accountType];
                     [arrayOfAccounts retain];

                     // Populate the tableview
                     if ([arrayOfAccounts count] > 0)
                         NSLog(@"print %@",[arrayOfAccounts objectAtIndex:0]);
                     //
                     ACAccount *twitterAccount = [arrayOfAccounts objectAtIndex:0];

                     NSString *userID = [[twitterAccount valueForKey:@"properties"] valueForKey:@"user_id"];
                     NSLog(@"print user id is %@",userID);// Here i can see immediately 

                    testLabel.text=[NSString stringWithFormat: @"Hi ,%@",userID];// Here it is taking more time...
4

2 に答える 2

2

非同期スレッドからUI要素を更新しています。そのため、問題が発生します。

交換:

testLabel.text=[NSString stringWithFormat: @"Hi ,%@",userID];

と:

dispatch_sync(dispatch_get_main_queue(), ^{

   testLabel.text=[NSString stringWithFormat: @"Hi ,%@",userID];

});

注意:UI要素はメインスレッドからのみ更新する必要があります。

あなたの場合、コードを内部に記述した場合block、ブロックはメインスレッドではなく非同期スレッドで実行されます。そこからUI要素を更新しています。これにより問題が発生するため、を使用しているため、メインスレッドからUIを更新する必要がありますdispatch_get_main_queue

于 2013-03-05T05:52:34.453 に答える
0

完了ブロックがメインスレッドで実行されていることを確認する必要があります。

以下のコードを試してください。

dispatch_async(dispatch_get_main_queue(), ^{
  testLabel.text=[NSString stringWithFormat: @"Hi ,%@",userID];
});
于 2013-03-05T05:56:45.480 に答える