簡體   English   中英

'NSInternalInconsistencyException',原因:'只在主線程上運行!' 錯誤

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

所以我知道這個錯誤的原因,但我不確定如何繞過它。

我有一個TextField ,我想不能編輯,但在視圖中啟用滾動。

這是我設置該視圖的方式:

    - (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];
        }
    }];

這是視圖的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操作(比如在標簽中設置文本,在圖像視圖中設置圖像,推動視圖控制器)。

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

Swift 3.0版

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

您必須始終在主線程上執行UI代碼。 你不是,因此錯誤。 嘗試這個:

[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];
        });
    }
}];

這可確保視圖控制器的顯示位於主線程上。 實際上,您可能需要在調用dispatch_async更多代碼。

Swift版本

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

而且,這不是一個在didSelectRow中做事的好方法。 你應該有一個網絡層,將網絡代碼與視圖控制器分開,否則你將得到巨大的視圖控制器,代碼將無法測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM