0

我用AFNetworking將數據發送到一個刪除Web服務,具有以下塊:AFJSONRequestOperation,更新UI如果操作成功

self.operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 

    [self updateAfterPost]; 


} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id jsonObject) { 
    [handle error] 
}]; 

- (void)updateAfterPost 
{ 
    if ([AppController sharedAppController].currentUser.twitterAccount.crossPost.boolValue == YES) { 
     [self postToTwitter]; 
    } 
    [other things that should update the UI] 
} 

- (void)postToTwitter 
{ 
    ACAccountType *accountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
    [self.accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { 
     if (granted == YES) {    
      if ([self.twitterAccounts count] > 0) { 
       ACAccount *twitterAccount = [self.twitterAccounts objectAtIndex:[AppController sharedAppController].currentUser.twitterAccount.accountId.integerValue]; 
       NSDictionary *message = @{@"status":self.postTextField.text}; 
       NSURL *requestURL = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"]; 
       SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestURL parameters:message]; 
       postRequest.account = twitterAccount; 
       [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
       }]; 
      } 
     } 
    }]; 
} 

postToTwitter動作導致以下警告(沒有警告,如果我取消註釋方法):

Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread. 

我不確定如何解決它。我在成功塊中嘗試過NSNotification,但沒有運氣(通知可能也在後臺線程中發送)。任何想法或建議?

更新 我也試過[self performSelectorOnMainThread:@selector(updateAfterPost) withObject:nil waitUntilDone:YES];

回答

1

在任意隊列上調用完成塊requestAccessToAccountsWithType,因此您無法在其中調用UI元素(您的postTextField)。

你應該換用dispatch_async調用的代碼:

- (void)postToTwitter 
{ 
    ACAccountType *accountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
    [self.accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
     if (granted == YES) {    
      if ([self.twitterAccounts count] > 0) { 
       ACAccount *twitterAccount = [self.twitterAccounts objectAtIndex:[AppController sharedAppController].currentUser.twitterAccount.accountId.integerValue]; 
       NSDictionary *message = @{@"status":self.postTextField.text}; 
       NSURL *requestURL = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"]; 
       SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestURL parameters:message]; 
       postRequest.account = twitterAccount; 
       [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
       }]; 
      } 
     } 
     }); 
    }]; 
} 
+0

解決了這個問題,小記:我必須創建一個塊變量爲我的'self.postTestField.text'和使用該變量的塊。 – Anders

相關問題