ios - 'NSInternalInconsistencyException',原因 : 'Only run on the main thread!' error

标签 ios iphone objective-c multithreading cocoa-touch

所以我知道这个错误的原因,但我不确定如何解决它。

我有一个 TextField,我希望它不可编辑,但在 View 中启用了滚动。

这就是我设置该 View 的方式:

    - (void)tableView:(UITableView *) tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *component = self.resultsTuples[indexPath.row];
    NSString* serverUrl = @"http://staging.toovia.com";
    NSString* pageLink = component[@"$element"][@"ContactCard"][@"PageLink"];
    NSString* recUrl =[NSString stringWithFormat:@"%@%@&format=json&fields=summary&fields=session_info", serverUrl,pageLink];

    [AJAXUtils getAsyncJsonWithUrl:(NSURL *)[NSURL URLWithString:recUrl]  callback:^(NSDictionary *returnjson){
        if (returnjson != nil){
            NSLog(@"RETURN JSON : %@", returnjson);
            NSString *userPageLink = returnjson[@"Node"][@"SessionInfo"][@"PostingAs"][@"Key"];
            self.detailController.userPageLink = userPageLink;
            self.detailController.nodePage = returnjson[@"Node"][@"Key"];
            NSString *selectedCard = component[@"$element"][@"Title"];
         //   [self.detailController setDescription:component[@"element"][@"ContactCard"][@"Description"]];
            [self.detailController setPageTitle:selectedCard];
            NSString* photoUrl = component[@"$element"][@"ContactCard"][@"Cover"][@"Medium"][@"Link"];
            [self.detailController setRestaurantImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoUrl]]]];



            self.detailController.title = selectedCard;

            NSString* rating = component[@"$element"][@"Summary"][@"AverageRating"];
            self.detailController.rating =(NSInteger)rating;
            [self.navigationController pushViewController:self.detailController animated:YES];
        }
    }];

这是 View 的 viewWillAppear 代码:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    self.rateView.notSelectedStar =[UIImage imageNamed:@"star-off.png"];
    self.rateView.fullSelectedStar = [UIImage imageNamed:@"star-on.png"];
    self.rateView.rating = self.rating;
    self.rateView.editable = YES;
    self.rateView.maxRating = 5;
    self.rateView.delegate = self;
    _pageTitleLabel.text = _pageTitle;
    _pageScoreLabel.text = _pageScore;
    _trendingImageView.image = [UIImage imageNamed:@"trending.png"];
    _restaurantImageView.image = _restaurantImage;

}

完整的错误是——

    2013-12-16 01:22:50.362 ReviewApp[7332:441f] *** Assertion failure in void _UIPerformResizeOfTextViewForTextContainer(NSLayoutManager *, UIView<NSTextContainerView> *, NSTextContainer *, NSUInteger)(), /SourceCache/UIFoundation_Sim/UIFoundation-258.1/UIFoundation/TextSystem/NSLayoutManager_Private.m:1510
2013-12-16 01:22:50.365 ReviewApp[7332:441f] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'

所以我猜这是因为文本字段的大小调整(当设置内容时)必须在主线程上发生。我如何强制发生/抑制此错误?我无法控制文本字段的大小调整,对吗?

谢谢

最佳答案

我猜测来自 AJAXUtils 的回调发生在后台线程上。我不知道这是否是您的代码,但您应该在主线程上执行所有 UI 操作(例如在标签中设置文本、在 ImageView 中设置图像、推送 View Controller )。

dispatch_sync(dispatch_get_main_queue(), ^{
    /* Do UI work here */
});

关于ios - 'NSInternalInconsistencyException',原因 : 'Only run on the main thread!' error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20597493/

相关文章:

ios - 如何检测来自 iOS native 应用程序中推文的链接

iphone - 管理 iOS 企业开发者计划

ios - Theos实例方法调用

iphone - 为什么 Cocoa 偶尔会返回一个空字符串?

ios - 启动swift后复制数据库

ios - 使用蓝牙的 iOS 应用程序是否需要蓝牙 SIG 资格?

ios - 使用 Realm 加密功能是否会对应用商店提交产生影响?

ios - 无法在 bundle 中加载 NIB

iphone - 关于数据模型的思考

objective-c - 将选择动画添加到 UIButton