NetworkActivityIndicatorVisible not working (synchronous request)

-(IBAction) webRequest; { response = [[NSMutableData alloc] init]; NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:kResourcesURL] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 10]; [theRequest setValue:@"application/json" forHTTPHeaderField:@"accept"]; [theRequest setValue:strToken forHTTPHeaderField:@"token"]; //show network activity indicator [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; response = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlresponse error:&nserror]; NSString *strResponse = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; SBJsonParser *sbjasonparser = [[SBJsonParser alloc] init]; arrResponse = [sbjasonparser objectWithString:strResponse error:nil]; //hide network activity indicator [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } 

Is there something wrong with my code? or does a synchronized request matter? Any suggestion?? Or should I use the AcivityIndicator view?

+4
source share
1 answer

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.

+4
source

Source: https://habr.com/ru/post/1389209/


All Articles