Pregunta

I use AFNetworking to send data to a remove web service, with the following block:

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) {
                 }];
            }
        }
    }];
}

The postToTwitter action results in the following warning (no warning if I uncomment the method):

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.

I'm not sure how to solve it. I have tried NSNotification in the success block but no luck (the notification is probably sent in the background thread as well). Any ideas or suggestions?

Update I have also tried [self performSelectorOnMainThread:@selector(updateAfterPost) withObject:nil waitUntilDone:YES];.

¿Fue útil?

Solución

The completion block of requestAccessToAccountsWithType is called on an arbitrary queue so you can't call a UI element (your postTextField) in it.

You should wrap the code with dispatch_async call:

- (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) {
                 }];
            }
        }
        });  
    }];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top