Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-29-2010, 03:11 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 17
pobri19 is on a distinguished road
Default ActivityIndicator Won't Appear!

Hi guys, I'm having a VERY tough time getting an activity indicator to appear.

I have a button that is pressed, and basically when the button is pressed it's linked to a method called process, when process runs I'm assigning the ActivityIndicator object an activity indicator, and attemting to addSubView it, then start animation, so the method will finish processing and at the end do StopAnimation etc.

For some reason it doesn't even display at all... I've googled heaps of examples and they all appear to be doing it the same way, here is a snippet of code:

P.S the payment client object contains a whole bunch of C functions that are being called from withing that "DO PROCESSING HERE" section. I'm not multi threading anything if you're curious.

Code:
// .h file

@interface MainViewController : UIViewController {
   UIActivityIndicatorView * activityIndicator;
   PaymentClient * paymentClient;
}

@property (nonatomic, retain) UIActivityIndicatorView * activityIndicator;
@property (nonatomic, retain) PaymentClient * paymentClient;

- (IBAction)processPayment:(id)sender;


// .m file


@synthesize activityIndicator;

- (void)processPayment:(id)sender {
   PaymentClient * tempPaymentClient = [[PaymentClient alloc] init];
   paymentClient = tempPaymentClient;
   [tempPaymentClient release];
   
   
   activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
   activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
   activityIndicator.center = self.view.center;
   [self.view addSubview: activityIndicator];

   [activityIndicator startAnimating];

   // DO PROCESSING HERE

   [activityIndicator stopAnimating];
   [activityIndicator release];
	return;   
}
Thanks for your help!
pobri19 is offline   Reply With Quote
Old 07-29-2010, 07:51 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

If the processing is a long task done on the main thread, the UIActivityIndicator won't appear. You should do any long tasks on a background thread, then notify the UI of the changes on a background thread.

This is a sample:
Code:
- (void)processPayment:(id)sender {
   PaymentClient * tempPaymentClient = [[PaymentClient alloc] init];
   [self setPaymentClient:tempPaymentClient];
   [tempPaymentClient release];
   
   
   activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
   activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
   activityIndicator.center = self.view.center;
   [self.view addSubview: activityIndicator];

   [activityIndicator startAnimating];
}
- (void)theProcess {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   // do the process
   [self performSelectorOnMainThread:@selector(processDone) withObject:nil waitUntilDone:NO];
   [pool release];
}
- (void)processDone {
   [activityIndicator stopAnimating];
   [activityIndicator release];
}
Also, no need for "return;" at the end of a function, because it's returning anyway and if you're not going to use your activityIndicator property, there's no need for it.

Last edited by harrytheshark; 07-29-2010 at 07:54 AM.
harrytheshark is offline   Reply With Quote
Old 07-29-2010, 06:20 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 17
pobri19 is on a distinguished road
Default

Quote:
Originally Posted by harrytheshark View Post
If the processing is a long task done on the main thread, the UIActivityIndicator won't appear. You should do any long tasks on a background thread, then notify the UI of the changes on a background thread.

This is a sample:
Code:
- (void)processPayment:(id)sender {
   PaymentClient * tempPaymentClient = [[PaymentClient alloc] init];
   [self setPaymentClient:tempPaymentClient];
   [tempPaymentClient release];
   
   
   activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
   activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
   activityIndicator.center = self.view.center;
   [self.view addSubview: activityIndicator];

   [activityIndicator startAnimating];
}
- (void)theProcess {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   // do the process
   [self performSelectorOnMainThread:@selector(processDone) withObject:nil waitUntilDone:NO];
   [pool release];
}
- (void)processDone {
   [activityIndicator stopAnimating];
   [activityIndicator release];
}
Also, no need for "return;" at the end of a function, because it's returning anyway and if you're not going to use your activityIndicator property, there's no need for it.
Thanks for your reply Harry! I'll give it a crack and report back if I have any dramas.

I'm guessing I need to create a new thread and parse in the theProcess function in the processPayment method?

As for the return statement, I'm coming from a C background

Cheers

Last edited by pobri19; 07-29-2010 at 07:50 PM.
pobri19 is offline   Reply With Quote
Old 07-30-2010, 04:15 PM   #4 (permalink)
Registered Member
 
msencenb's Avatar
 
Join Date: May 2009
Location: Stanford, CA
Posts: 291
msencenb is on a distinguished road
Default

Harry is correct.

Anytime you are doing a lot of processing/downloading of data you will want to do it on a different thread. Doing it on the main thread will lock up the UI which means not only will the activityIndicator not appear but the user won't be able to do anything else either. To the users eyes the app is frozen and more than likely they will simply close it which is certainly bad for business.
__________________
I'm starting a new blog dedicated to iOS development. Check it out at:

http://www.iosdevscreencasts.com
msencenb is offline   Reply With Quote
Old 08-02-2010, 06:51 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 17
pobri19 is on a distinguished road
Default

Yeah that solved the problem :-) Your example was really helpful harry, thank you!
pobri19 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 470
14 members and 456 guests
7twenty7, AlanFloyd, David-T, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0