methods - Objective-C, how to display determinate progress bar? -


i show progress bar in app determinate rather indeterminate. doesn't work though when setting determinate (works fine indeterminate). i've read some of other answers this, although haven't worked. appreciated - thanks!

@interface appdelegate : nsobject <nsapplicationdelegate> {      iboutlet nsprogressindicator *showprogress;  }  - (ibaction)somemethod:(id)sender {      [showprogress setusesthreadedanimation:yes];   // works     [showprogress startanimation:self];            // works     [showprogress setdoublevalue:(0.1)];           // not work     [showprogress setindeterminate:no];            // not work      [self dosomething];     [self dosomethingelse];     [self dosomethingmore];     ....      [barprogress setdoublevalue:(1.0)];            // not work     [barprogress stopanimation:self];              // works  } 

updated code [working]:

- (ibaction)somemethod:(id)sender {      [showprogress setusesthreadedanimation:yes];     [showprogress startanimation:self];     [showprogress setindeterminate:no];      [showprogress setdoublevalue:(0.1)];     [showprogress startanimation:nil];      dispatch_queue_t backgroundqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);     dispatch_async(backgroundqueue, ^{          (nsuinteger = 0; < 1; i++) {              dispatch_async(dispatch_get_main_queue(), ^{                 [barprogress incrementby:10.0];             });         }      [self dosomething];              [showprogress incrementby:...];          dispatch_async(dispatch_get_main_queue(), ^{              [showprogress stopanimation:nil];          });     });      [showprogress setdoublevalue:(1.0)]; } 

your dosomething method blocking main thread, causes run loop not cycle, in turn causes ui redraw blocked. fix long running work in dosomething on background queue, periodic callbacks main queue update progress bar.

i have no idea dosomething method does, sake of explanation, let's assume runs loop 100 steps. you'd implement this:

- (void)dosomething {     [showprogress startanimation:nil];     dispatch_queue_t backgroundqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);     dispatch_async(backgroundqueue, ^{         (nsuinteger = 0; < 100; i++) {             // whatever need              dispatch_async(dispatch_get_main_queue(), ^{                 [showprogress incrementby:1.0];             });         }         // done long running task         dispatch_async(dispatch_get_main_queue(), ^{             [showprogress stopanimation:nil];         });     }); } 

keep in mind, still need set progress indicator determinate, initialize value , set appropriate minvalue , maxvalue.

if must work in dosomething on main thread, it's possible schedule small chunks of work done on each run loop cycle, or manually spin run loop periodically you're doing work, grand central dispatch (gcd) first choice if can use it.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -