HardCode
HardCode

Reputation: 2025

How to use UIActivityIndicatorView when ASIHTTPRequest load data?

I'm loading JSON data using ASIHTTPRequest. I'm requesting huge amount of data when the user touch the button (Here I'm loading another view which will request JSON data). It takes about 15 sec. to load. How can I use the UIActivityIndicatiorView once the user touch the button until the data loads.

Anyone did this before or any suggestion to improve user performance?

Upvotes: 0

Views: 866

Answers (2)

mhunturk
mhunturk

Reputation: 296

    -(void) startRequest:(NSString*)username password:(NSString*)password
{
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:mainUrl];    
    [request setDelegate:self]; 
    [request setDidFailSelector:@selector(requestFailed:)];
    [request startAsynchronous];
    [self.addSubview:self.indicator];
    [indicator startAnimating];

}

    -(void) requestFinished: (ASIHTTPRequest *) request {

    [self.indicator stopAnimating];
    [self.indicator removeFromSuperview];

}

Upvotes: 1

Mobilewits
Mobilewits

Reputation: 1763

Here is a sample for using the ASIHTTPRequest and UIActivityIndicator

-(void) loadImageFromURL:(NSURL *) url withActivityIndicator:(BOOL) yesOrNo {        
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

UIActivityIndicatorView *activityIndicator = nil;

if (yesOrNo == YES) {
    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
    [activityIndicator setCenter: self.center];
    [activityIndicator startAnimating];
    [self addSubview: activityIndicator];
}

[request setCompletionBlock: ^{
    self.image = [UIImage imageWithData: [request responseData]];
    [activityIndicator removeFromSuperview];
    [activityIndicator release];
}];

[request setFailedBlock: ^{
    [activityIndicator removeFromSuperview];
    [activityIndicator release];
}];

[request startAsynchronous];
}

@end

Upvotes: 0

Related Questions