I assume that the indicator of network activity requires a layout of some types, i.e. when you make it (in) visible, it sends out [self setNeedsLayout] at some point. This method does not lead to the immediate creation of a layout; it simply marks the view as a necessary layout with the actual layout occurring at the end of the execution cycle. The problem in your case is that you block the main thread with a synchronous request, so that the end of the start cycle occurs after the indicator has become invisible again.
There is only one way to avoid blocking the main thread: make it asynchronous in terms of the main thread. You can
- Use the asynchronous API NSURLConnection
- Use the synchronous NSURLConnection API in the background thread
- Use the synchronous NSURLConnection API with NSOperationQueue.
The idea is to show an indicator of network activity, start a network request, allow the main thread to scroll through the loop, and hide the indicator when the request is complete.
source share