gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:46:53 25 4
gpt4 key购买 nike

我使用 AFNetworking 将数据发送到删除 Web 服务,并使用以下 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) {
}];
}
}
}];
}

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.

我不知道如何解决这个问题。我在成功 block 中尝试过 NSNotification 但没有运气(通知也可能在后台线程中发送)。有什么想法或建议吗?

更新我也尝试过 [self PerformSelectorOnMainThread:@selector(updateAfterPost) withObject:nil waitUntilDone:YES];

最佳答案

requestAccessToAccountsWithType 的完成 block 在任意队列上调用,因此您无法在其中调用 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) {
}];
}
}
});
}];
}

关于ios - AFJSONRequestOperation,如果操作成功则更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17639059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com