开发者

Network activity indicator problem

开发者 https://www.devze.com 2023-02-10 18:46 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消