ios - 在 slrequest performrequestwithhandler 使用 twitter 句柄停止后更新 uitableviewcell 中的文本字段

标签 ios objective-c uitableview twitter slrequest

我正在尝试提取用户的 Twitter 句柄并将其填充到表格 View 中的文本字段中。请求成功完成并调用执行 block ,但 UI 需要几秒钟才能更新。我试过使用 NSNotifications 来触发 UI 更改,但在更新 UI 时仍然有延迟。下面是我用来从 ACAccountStore 中提取信息的代码,我为表格使用的唯一特殊类是自定义 UITableViewCell。还有其他人看到这个吗?还是我遗漏了导致运行时问题的内容?

- (IBAction)connectTwitter:(id)sender {
      UISwitch *sw = (UISwitch *)sender;
      if (sw.isOn == NO) {
           return;
      }    
     ACAccountStore *account = [[ACAccountStore alloc] init];
     ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    __weak MBSuitUpViewController *weakSelf = self;
    [account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
        if (granted == YES) {
            NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
            if (arrayOfAccounts.count > 0) {
                ACAccount *twitterAccount = [arrayOfAccounts lastObject];
                NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1/account/verify_credentials.json"];
                SLRequest *user = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:requestURL parameters:nil];
                user.account = twitterAccount;
                [user performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
                    [weakSelf twitterDictionaryHandler:json];
                    weakSelf.userInfo = @{@"name": json[@"name"], @"handle":json[@"screen_name"]};
            }];
            } else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"No Twitter Account Found" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Okay", nil];
                [alert show];
            }
        } else {
            NSLog(@"Permission denied %@", error.localizedDescription);
        }
    }];
}

- (void)twitterDictionaryHandler:(NSDictionary *)dictionary {
    MBSuitUpCell *username = (MBSuitUpCell *)[self.view viewWithTag:2];
    username.textField.text = dictionary[@"screen_name"];
    NSLog(@"Should update textfield");
}

最佳答案

它真的会立即调用“twitterDictionaryHandler”函数吗?

如果它这样做了,但有一段时间没有更新 UI,那么这似乎是 iOS 的一个常见问题。您需要做的是将 UI 的更改放入 GCD(Grand Central Dispatch)Main Queue,以便它具有优先级并尽快完成。我在下面为可能遇到此问题的其他人举了一个例子。

dispatch_async(dispatch_get_main_queue(), ^{
    username.textField.text = dictionary[@"screen_name"]; 
}); 

关于ios - 在 slrequest performrequestwithhandler 使用 twitter 句柄停止后更新 uitableviewcell 中的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22025360/

相关文章:

ios - 启动屏幕 Storyboard 的图像消失

ios - 当他们要求提供应用内购买截图时,苹果到底在寻找什么?

iphone - 检测 iOS5 上何时禁用蓝牙

objective-c - 如何在 iOS >= 4.0 的 Objective-C 中创建私有(private)方法和 ivars?

swift - 编辑核心数据列表项追加而不是添加

ios - 如何使用 UISegementedControl -> Swift 将不同的数据类从 Parse 加载到 uitableview 中?

ios - NSHomeDirectory 为守护进程返回什么?

iphone - 未调用核心数据验证方法 - iPhone

objective-c - addObserverForName 并删除观察者

uitableview - 如何像在 iPhone 日历应用程序中一样创建日历 View