tags:

views:

57

answers:

3

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

1) In ViewDidLod

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

[self userDataFromServer];

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

----OR-----

2) In ViewDidLod

[self startActivityIndicator];

[self userDataFromServer];

[self stopActivityIndicator];

Both are working in the say way which is not correct. Can any one help me how we can use activityindicator on parallel thread.

+1  A: 

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];
}
Brandon Schlenker
autoreleased with no pool in place - just leaking. Its showing this in log, how I can remove it
iPhoneDev
Add an NSAutorelease pool like I showed above and that will solve the issue. Place any code in between NSAutoreleasePool *pool.... and [pool release];
Brandon Schlenker
A: 

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.

Thomas Clayson
I am sending this: NSData* data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:
iPhoneDev
thats your problem... don't send a synchronous request - it will delay the iphone's reactions. ie: it will freeze the UI momentarily - including the activity indicator. Look up NSURLConnection's delegate methods and use `NSURLConnection *conn = [NSURLConnection connectionWithRequest:urlRequest];` there is a delegate method called `-connection:didRecieveData:` where you can `[myData appendData:data];` where `myData` is a global `NSMutableData` variable. :) That way it will run in the background and won't interrupt your app from running.
Thomas Clayson
I can't change this class. Please help me to know how I can use activity indicator with SynchronousRequest. I tried to use NSAutoreleasePool but its showing "autoreleased with no pool in place - just leaking".
iPhoneDev
you can't do it... the problem is that everything sent to the screen is done on the main thread. If you're blocking that thread with synchronous requests then the activity indicator won't display/animate until you've stopped the request - ie: until its finished. Why can't you change the class?
Thomas Clayson
Its in network layer, if I change it every thing will going to affect. I am in last stage of my project and don't want any drastic change in core logic. For I have implemented all the business functionality based on synchronous request. I don't know what will happen if change it to asynchronous :(
iPhoneDev
A: 

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;
}
notme