0

Я использую AFNetworking для отправки данных на удаление веб-службы, со следующим блоком:AFJSONRequestOperation, обновление пользовательского интерфейса, если операция прошла успешно

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 вызывается в произвольной очереди, поэтому вы не можете вызвать элемент интерфейса (ваш посттекстовый фильтр) в нем.

Вы должны обернуть код с 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

Смежные вопросы