Im trying to show the UIActivityIndicator on my NavigationBar in this MapView with the following code. However, no matter which way I try I cannot show it. Whats the trick here? Also, I am trying to do the map annotations fetching from server and showing on the map in the background, is this the way to do it?
- (void)viewDidLoad
{
[super viewDidLoad];
self.activityIndicatorView = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
self.activityIndicatorView.hidesWhenStopped = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:self.activityIndicatorView];
[self.activityIndicatorView startAnimating];
[self performSelectorInBackground:@selector(loadMapAnnotations) withObject: nil];
[self.activityIndicatorView stopAnimating];
}
//=========================================================================
-(void)loadMapAnnotations
{
self.mapView.showsUserLocation = YES;
if(self.vManager = [VendorManager getVendorManager])
{
NSLog(@"VendorManager = %@",[self.vManager description] );
[self.vManager vendorsNearLocation:userLocation block:^(NSArray *vendors,
NSError *error)
{
if(vendors && [vendors count])
{
for (id v in vendors)
{
Vendor *aVendor = [[Vendor alloc] initWithAttributes:v];
NSLog(@"Vendor from Vendors = %@",[aVendor name]);
[self.mapView addAnnotation:aVendor];
}
}
else
{
NSLog(@"Failed to get vendors: %@", [error description] );
}
}];
}
}
You’re hiding the activity indicator immediately after you show it.
The problem is that you think that
performSelectorInBackground:withObject:waits for the method to complete – I don’t really understand why you think it does, since it implicitly has its asynchronous behavior in its name…Nevertheless, moving the
[self.activityIndicatorView stopAnimating];call to the callback block will solve this “problem”.