简体   繁体   English

'NSInternalInconsistencyException',原因:'只在主线程上运行!' 错误

[英]'NSInternalInconsistencyException', reason: 'Only run on the main thread!' error

So I know the reason for this error, but I'm not really sure how to get around it. 所以我知道这个错误的原因,但我不确定如何绕过它。

I have a TextField that I want to not be editable, but with scrolling enabled inside a view. 我有一个TextField ,我想不能编辑,但在视图中启用滚动。

This is how I set up that 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];
        }
    }];

This is the viewWillAppear code for the 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;

}

The full error is - 完整的错误是 -

    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!'

So I guess it's happening because the resizing of the text field (when content is set) has to happen on the main thread. 所以我猜它正在发生,因为文本字段的大小调整(当设置内容时)必须在主线程上发生。 How do I force this to happen/suppress this error? 如何强制这种情况发生/抑制此错误? I'm not in control of the text field resizing am I? 我不能控制文本字段调整大小吗?

Thanks 谢谢

I'm guessing that the callback from AJAXUtils happens on a background thread. 我猜测来自AJAXUtils的回调发生在后台线程上。 I don't know if that is your code or not but you should do all UI operations (like setting text in a label, setting an image in an image view, pushing a view controller) on the main thread. 我不知道这是不是你的代码,但你应该在主线程上做所有的UI操作(比如在标签中设置文本,在图像视图中设置图像,推动视图控制器)。

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

Swift 3.0 version Swift 3.0版

DispatchQueue.main.async(execute: {
    /* Do UI work here */
})

You must always perform UI code on the main thread. 您必须始终在主线程上执行UI代码。 You are not, hence the error. 你不是,因此错误。 Try this: 尝试这个:

[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;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.navigationController pushViewController:self.detailController animated:YES];
        });
    }
}];

This ensures the display of the view controller is on the main thread. 这可确保视图控制器的显示位于主线程上。 You may actually need to put a bit more of the code inside the call to dispatch_async . 实际上,您可能需要在调用dispatch_async更多代码。

Swift version Swift版本

dispatch_async(dispatch_get_main_queue()){
      /* Do UI work here */
}

Also, it's not a very good way of doing things in didSelectRow. 而且,这不是一个在didSelectRow中做事的好方法。 You should have a network layer, separate the network code from view controllers otherwise you will end up with huge view controller, code won't be testable. 你应该有一个网络层,将网络代码与视图控制器分开,否则你将得到巨大的视图控制器,代码将无法测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xcode 9.0 Swift 04(由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“仅在主线程上运行!”) - Xcode 9.0 Swift 04 (Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!') 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“仅在主线程上运行!” - Terminating app due to an uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!' 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“线程违规:预期主线程” - Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'threading violation: expected the main thread' 仅在主线程上运行异常 - Only run on the main thread exception iOS Swift仅在应用程序的主线程错误上运行 - iOS Swift Only run on the main thread error for app NSInternalInconsistencyExceptionException访问_cachedSystemAnimationFence需要主线程 - NSInternalInconsistencyException accessing _cachedSystemAnimationFence requires the main thread 错误:“ NSInternalInconsistencyException”,原因:“ outputURL不能为NULL” - Error : 'NSInternalInconsistencyException', reason: 'outputURL cannot be NULL' iOS &#39;NSInternalInconsistencyException&#39;,原因:&#39;UICollectionViewFlowLayout 内部错误 - iOS 'NSInternalInconsistencyException', reason: 'UICollectionViewFlowLayout internal error Swift错误:NSInternalInconsistencyException,&#39;试图在非主队列上执行的上传! 仅在主队列上。” - Swift Error: NSInternalInconsistencyException, 'Upload attempting to execute on non main queue! Only on the main queue.' “仅在主线程上运行”异常仅限于模拟器 - “Only run on the main thread” exception is simulator only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM