首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AFJSONRequestOperation,如果操作成功,更新UI

AFJSONRequestOperation,如果操作成功,更新UI
EN

Stack Overflow用户
提问于 2013-07-14 11:42:49
回答 1查看 97关注 0票数 0

我使用AFNetworking将数据发送到remove web服务,并使用以下块:

代码语言:javascript
复制
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操作将产生以下警告(如果取消对方法的注释,则没有警告):

代码语言:javascript
复制
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];

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-14 12:14:28

requestAccessToAccountsWithType的完成块是在任意队列上调用的,因此不能在其中调用UI元素(您的postTextField)。

您应该用dispatch_async调用包装代码:

代码语言:javascript
复制
- (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) {
                 }];
            }
        }
        });  
    }];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17639059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档