objective c - Asynchronous dispatch in a called method -
i trying complete long calculation determinate progress bar. working fine below code. however, in method calls scan
, moves on scorewithequation
before scan
has finished updating storedlibrary
object. i've tried different methods waiting dispatch queues finish, end hanging ui. need using callback? i've looked solution, don't understand how pass arguments , return variables , forth.
-(void) scan{ if (!_sheet){ [nsbundle loadnibnamed: @"progresssheet" owner: self]; } [nsapp beginsheet: _sheet modalforwindow: [[nsapp delegate]window] modaldelegate: self didendselector: null contextinfo: nil]; [_sheet makekeyandorderfront:self]; [_progressbar setindeterminate:yes]; [_progressbar setminvalue:0]; [_progressbar startanimation:self]; __block nsmutablearray *temptracks = [[nsmutablearray alloc]init]; //do stuff dispatch_async(dispatch_get_global_queue(0, 0), ^{ //do stuff [_progressbar setdoublevalue:0.f]; [_progressbar setmaxvalue:[listtracks count]]; [_progressbar setindeterminate:no]; (itunestrack *listtrack in listtracks) { //do stuff temptracks } dispatch_async(dispatch_get_main_queue(), ^{ [_progressbar incrementby:1]; }); } } dispatch_async(dispatch_get_main_queue(), ^{ [_progressbar setindeterminate:yes]; [nsapp endsheet:self.sheet]; [self.sheet orderout:self]; self.listtracks = temptracks; }); }); }
below code calls scan
:
- (ibaction)evaluate:(id)sender { if (self.storedlibrary == nil){ self.storedlibrary = [[storedlibrary alloc] init]; [self.storedlibrary scan]; } self.tabletracks = [self.storedlibrary scorewithequation:[_scoringequation stringvalue] sortorder:[_sortorder titleofselecteditem] tracklimit:[_tracklimit integervalue]]; }
i keep scan
, scorewithequation
separate methods, called elsewhere separately.
when use dispatch_async block dispatched not block current thread, , execution continue immediatly.
in case, when call scan, stuff , call dispatch_async other stuff. in dispatch block not executed in run loop , [self.storedlibrary scan] returns scorewithequation executed immediatly.
if want block current thread (the thread on dispatch call executed) need use dispatch_sync.
if want execute scorewithequation after scan, can create serial_queue, , dispatch_async both scan , scorewithequation custom serial_queue. doing execution not block current thread, , executed in serie.
check "creating serial dispatch queue" ( http://developer.apple.com/library/ios/#documentation/general/conceptual/concurrencyprogrammingguide/operationqueues/operationqueues.html )
or retaining completion block execute scorewithequation callback possibility.
Comments
Post a Comment