Im trying to set a UITextView's text property with this code but I get a crash saying I can't do it from the main thread:
__block NSString *stringForText;
self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// ...
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
int errorCode = httpResponse.statusCode;
NSString *errorStatus = [NSString stringWithFormat:@"%d",errorCode];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *totalResponse = [errorStatus stringByAppendingString:responseString];
stringForText = totalResponse;
[self updateView:stringForText];
// 4
self.uploadView.hidden = NO;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}];
// 5
[_uploadTask resume];
}
-(void)updateView:(NSString*)texto{
self.myTextView.text = texto;
}
Why does it crash saying I can't call it from the main thread in TestFlight?
Check again. You must update the UI from the main thread. It is likely your upload completion handler is operating off of the main thread. If you would like to call updateView:
from any thread, then the you can dispatch the manipulation of the label to the main thread:
-(void)updateView:(NSString*)texto{
dispatch_async(dispatch_get_main_queue(), ^{
self.myTextView.text = texto;
});
}