koichirose
koichirose

Reputation: 1825

setNetworkActivityIndicatorVisible spinner not displaying

As the title says, here's some code:

- (void)refreshMap {
    NSLog(@"refreshing");
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    lat = [[NSNumber numberWithDouble:myUserLocation.coordinate.latitude] stringValue];
    lon = [[NSNumber numberWithDouble:myUserLocation.coordinate.longitude] stringValue];

    NSString *url = [[NSString alloc] initWithFormat:@"http://mywebsite.com/geo_search.json?lat=%@&lon=%@", lat, lon];
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];
    [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    [url release];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

The network activity indicator (in the status bar) doesn't appear at all (not in the Simulator, nor on a real device). How do I solve this?

Upvotes: 2

Views: 6253

Answers (1)

Ole Begemann
Ole Begemann

Reputation: 135548

The UI won't be updated unless your code returns control to the runloop. So if you enable and disable the network indicator in the same method, it will never actually show. The solution is to call

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

in your url connection delegate methods (conectionDidFinishLoading: and connection:didFailWithError:).

Upvotes: 7

Related Questions