'NSInternalInconsistencyException', reason: 'Only run on the main thread!' error 'NSInternalInconsistencyException', reason: 'Only run on the main thread!' error multithreading multithreading

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


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 */});


Swift 3.0 version

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


You must always perform UI code on the main thread. 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.