Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios AFJSONRequestOperation,如果操作成功,则更新UI_Ios_Objective C_Concurrency_Afnetworking_Afjsonrequestoperation - Fatal编程技术网

Ios AFJSONRequestOperation,如果操作成功,则更新UI

Ios AFJSONRequestOperation,如果操作成功,则更新UI,ios,objective-c,concurrency,afnetworking,afjsonrequestoperation,Ios,Objective C,Concurrency,Afnetworking,Afjsonrequestoperation,我使用AFNetworking将数据发送到remove web服务,具有以下块: self.operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { [self updateAfterPost]; } failure:^(NSURLRequest

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

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) {
                 }];
            }
        }
    }];
}
posttowitter
操作会导致以下警告(如果我取消对方法的注释,则不会发出警告):

我不知道怎么解决它。我在成功块中尝试了
NSNotification
,但没有成功(通知可能也在后台线程中发送)。有什么想法或建议吗

更新
我还尝试了
[self-performSelectorOnMainThread:@selector(updateAfterPost)with-object:nil waitUntilDone:YES]

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

您应该使用dispatch\u 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) {
                 }];
            }
        }
        });  
    }];
}

小注:我必须为我的
self.postTestField.text
创建一个块变量,并在块中使用该变量。
- (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) {
                 }];
            }
        }
        });  
    }];
}