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 > iPhone SDK Development Forums > iPhone SDK Tutorials

Reply
 
LinkBack Thread Tools Display Modes
Old 06-10-2011, 05:28 PM   #1 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default How to display an activity indicator or other UI before beginning a long task

It seems like at least once a week somebody asks why they can't display a UIActivityIndicatorView (or alert view), then perform a time-consuming synchronous task.

They are mystified that the activity indicator doesn't show up until after the time-consuming task is complete.

The reason it doesn't work is this: Cocoa queues up the user interface changes you make in your code, and applies them the next time your code returns and the application visits the event loop. So, if you do this:
  1. start activity indicator
  2. do time-consuming work
  3. stop activity indicator
  4. return

Then the activity indicator doesn't actually display at all. The UI changes don't take place until after your code returns, and by then, the time-consuming work is over.

The key to fixing this is a method called performSelector:withObject:afterDelay:. That method lets you invoke a method in the future.

What you do is this:

Split out your time-consuming code into a separate method. Let's call the method doSomethingSlow.

Code:
- (IBAction) someMethod
{
  [theActivityIndicator startAnimating];  //Or whatever UI Change you need to make
  [self performSelector: @selector(doSomethingSlow) 
    withObject: nil 
    afterDelay: 0];
  return;
}

- (void) doSomethingSlow
{
   //perform time-consuming tasks
  [theActivityIndicator stopAnimating];  //Or whatever step to indicate that the task is done.
}

The code fragments above assume that you have already created an activity indicator view in interface builder and hooked it up as an outlet called theActivityIndicator.

Note that the exact same issue comes up with any user interface change you want to make before doing a time-consuming task, and the same solution works. Just change the line that starts the activity indicator animating to whatever UI change you want to make.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-22-2011, 07:15 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 5
Stormdog is on a distinguished road
Default

I followed this technique, and still do not see the indicator… ??

I am creating my indicator view programmatically, is that the problem?

UIActivityIndicatorView *activityIndicator;
activityIndicator = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicator ViewStyleGray] autorelease];
activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];


// This line starts the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

// This line starts the activity indicator on the view
[activityIndicator startAnimating];


[self performSelector: @selector(autoResume)
withObject: nil
afterDelay: 0];

// This line stops the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

// This line stops the activity indicator on the view, in this case the table view
[activityIndicator stopAnimating];




}
- (void) autoResume
{
//perform time-consuming tasks
}
Stormdog is offline   Reply With Quote
Old 09-22-2011, 07:43 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Stormdog View Post
I followed this technique, and still do not see the indicator… ??

I am creating my indicator view programmatically, is that the problem?

UIActivityIndicatorView *activityIndicator;
activityIndicator = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicator ViewStyleGray] autorelease];
activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];


// This line starts the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

// This line starts the activity indicator on the view
[activityIndicator startAnimating];


[self performSelector: @selector(autoResume)
withObject: nil
afterDelay: 0];

// This line stops the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

// This line stops the activity indicator on the view, in this case the table view
[activityIndicator stopAnimating];




}
- (void) autoResume
{
//perform time-consuming tasks
}
I don't think I've created an activity indicator through code before.

Your code looks reasonable. One thing I would change: Don't set the frame of your activity indicator. They're designed to draw at a fixed size. You're already using the center property to place it where you want it, so get rid of the assignment to the frame property.

I'd step through your code in the debugger and make sure you are getting back a valid activity indicator, and that it's hidden property is FALSE. Also make sure self.view is non-nil.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-22-2011, 03:28 PM   #4 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 5
Stormdog is on a distinguished road
Default

Verified above as suggested, all good; but, still NO JOY

Does it matter what kind of view/controller this is being pushed upon?
Stormdog 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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 477
14 members and 463 guests
alexeir, David-T, Dj_kades, foslock, iAppDeveloper, jeroenkeij, LunarMoon, Mijator, Pauluz85, pipposanta, QuantumDoja, robsmy, sacha1996, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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