开发者

Whis is right way to add activity indicator in iPhone

I need to show activity indicator but not able to find the right approach, what I tried is:

  1. In ViewDidLoad

[self performSelectorOnMainThread:@selector(setupActivityIndicator) withObject:nil waitUntilDone:NO];

[self userDataFromServer];

[self performSelectorOnMainThread:@selector(stopActivityIndicatorInMainThread) withObject:nil waitUntilDone:NO]

----OR-----

  1. In ViewDidLoad

[self startActivityIndicator];

[self开发者_开发问答 userDataFromServer];

[self stopActivityIndicator];

Both are working in the say way which is not correct. How can we use activityindicator on parallel thread?


In my experience, the best practice to download data from a server is

- (void)viewDidLoad {
   [self startActivityIndicator];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
                             [NSURLRequest requestWithURL:
                              [NSURL URLWithString:aRequest]] delegate:self];
    [conn release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // Store your data
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"ERROR DOWNLOADING");

    // Here you can display an UIAlert message

    // Then stop your activity indicator
    [self stopActivityIndicator];

    // Release the connection now that it's finished
    connection = nil;

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"FINISH DOWNLOAD");

    // Or just stop
    [self stopActivityIndicator];

    // Do something

    // Release the connection now that it's finished
    connection = nil;
}


There's no need to call performSelectorOnMainThread: in viewDidLoad because you're most likely already on the main thread. UIKit, which UIActivityIndicator is part of, always needs to be called from the main thread anyway.

I think what you may want to do is run userDataFromServer on a secondary thread. In viewDidLoad try

[self performSelectorInBackground:@selector(userDataFromServer) withObject:nil];

Then in your userDataFromServer method, make sure you include an NSAutoreleasePool

-(void)userDataFromServer {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        //code to actually get the data
       [pool release];
}


An activity indicator just shows that there is activity.

For instance if you're using NSURLConnection to run an NSURLRequest on a separate thread you would add the activity indicator to the view on viewDidLoad then do [activityIndicator startAnimating] when you do [urlConnection start]. Then when you get to -connectionDidFinish you can stop it. If you understand.

I'm assuming your problem is that you're doing something which is "hanging" the iphone in userDataFromServer so its probably best to perform THAT selector on another thread. Can you post that method up at all? If you're using NSURLRequest then if you add [NSURLConnection connectionWithRequest:myRequest] and look up the NSURLConnection delegate methods then this works asynchronously for you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜