praks5432 praks5432 - 1 year ago 95
Objective-C Question

'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

that I want to not be editable, but with scrolling enabled inside a view.

This is how I set up that view:

- (void)tableView:(UITableView *) tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *component = self.resultsTuples[indexPath.row];
NSString* serverUrl = @"";
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
code for the view:

[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?


Answer Source

I'm guessing that the callback from AJAXUtils happens on a background thread. 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.

dispatch_sync(dispatch_get_main_queue(), ^{
    /* Do UI work here */
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download