I have an application that has a toolbar with a UISegmentedControl nested inside of it as a subview. When I switch segment I done what follows:
/*
* Set/Reset dei dati.
*/
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
if ( bannerVideo ) {
[filmatiBanner release];
filmatiBanner = nil;
filmatiBanner = [[NSMutableArray alloc] initWithCapacity:0];
[[bannerVideo view] setHidden:YES];
[bannerVideo release];
bannerVideo = nil;
}
[lowerBannerActivity setHidden:NO];
[lowerBannerActivity startAnimating];
url = [NSURL URLWithString:@"http:/开发者_C百科/www.udc.ntx.it/filmatiBlocco2.asp"];
request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[[voci objectAtIndex:[switches selectedSegmentIndex]] objectForKey:@"codblocco"]
forKey:@"CodiceBlocco"];
[request startSynchronous];
// Bla bla yada yada
When the view loads I correctly see the UIActivityIndicator spinning and the NetworkActivity in the status bar above. But when I tap the switch, the app "freezes" until it done the job then fastly shows the spinner and then hides it. How should I get rid of this problem?
You should do loading the activity in another thread - you are doing this in the same (main) thread: startSynchronous indicates this. I think that ASI library supports loading this in a different thread, but I'm not so familiar with the library. (you can call something like
[request setDelegate:self];
[request startAsynchronous];
and then implement:
-(void)requestFinished:(ASIHTTPRequest *)request
and in that method remove the network activity indicator. )
Or you can do something like this (the hard way - start your own thread ;)) :
[NSThread detachNewThreadSelector:@selector(loadBanner:) toTarget:self withObject:param];
When the loadBanner method is over, you should return to your main thread that the banner is loaded, something like this:
(calling this from loadBanner method)
[self performSelectorOnMainThread:@selector(bannerIsLoaded:) withObject:rez waitUntilDone:NO];
and in the method bannerIsLoaded remove the network activity indicator and show the banner itself.
精彩评论