【问题标题】:AFJSONRequestOperation, update UI if operation is successfulAFJSONRequestOperation,如果操作成功则更新 UI
【发布时间】:2013-07-14 11:42:49
【问题描述】:

我使用 AFNetworking 将数据发送到删除网络服务,具有以下块:

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];

【问题讨论】:

    标签: ios objective-c concurrency afnetworking afjsonrequestoperation


    【解决方案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) {
                     }];
                }
            }
            });  
        }];
    }
    

    【讨论】:

    • 解决了,小提示:我必须为我的self.postTestField.text 创建一个块变量并在块中使用该变量。
    猜你喜欢
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多