Reputation: 6347
I am somewhat new to iOS development and am having an issue with threading. I am calling a web service that returns json data and the code to perform this action works as expected. For testing, i would like to be able to click a button, retrieve the data and populate a textview control with formatted results. Here is my code excerpted from a button click event handler:
dispatch_queue_t que = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(que, ^{
thisRiverGauge = [[RiverGauge alloc] initWithStationInfo:gauge forHistoryInHours:5 inThisFormat:@"json"];
[txtResults setText:rval];
});
When trying to update the textview (txtResults) from within the thread, I get a runtime error. When I place the update to the textview outside of the thread, obviously it won't update because the thread takes longer to complete than the execution of the event handler. What might be a solution to this?
Thx!
Upvotes: 1
Views: 100
Reputation: 12671
You should perform GUI related tasks on the main thread, add the main queue/thread block around the code where you are updating the value for textview.
ispatch_queue_t que = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(que, ^{
thisRiverGauge = [[RiverGauge alloc] initWithStationInfo:gauge forHistoryInHours:5 inThisFormat:@"json"];
dispatch_async(dispatch_get_main_queue(), ^{
[txtResults setText:rval];
});
});
Upvotes: 4