The problem is that the code is blocking the main thread until the network request finishes. The screen will only update after the sendk
method returns, but the method won't return until the sendSynchronousRequest
method is finished. The solution is to dispatch the networking code (everything after /*FIN HUD*/
) onto a background thread, or use sendAsynchronousRequest
, and use the completion block to notify the main thread when the response arrives.
The code framework for using a background thread looks like this
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// do networking stuff here
dispatch_async( dispatch_get_main_queue(), ^{
// turn off the HUD and remove the spinner here
// also do something with the network response here
});
});