objective c - iOS-Managing and Keeping Track of Multiple Concurrent tasks -


in app need load data multiple sources , put them in table view. gathering each of sources 1 after take forever. around need run of download operations together. since download tasks, in theory run them, issue part of code on thread runs asynchronously, means need main thread complete operation.

so in order of running in background, need use gcd, don't have experience with.

//dataloader.m  dispatch_async(dispatch_get_global_queue( dispatch_queue_priority_default, 0), ^(void){     [self.webloader getfeedwithcompletion:self.thatothercompletionblock];     [self.otherdataloader getdatawithcompletion:self.completionblock];     [self.thatdataloader getthatdatawithcompletion:self.anothercompletionblock]     dispatch_async(dispatch_get_main_queue(), ^(void){      }); }); 

however, since part of task asynchronous, need figure out put gcd code.

i put before starting task, did above. work, however, since tasks partially run in background (in cases cannot change that), seems wasteful running task runs partially in background in background. why run runs in background thread in thread?

another option use gcd in actual class gets feed (ex. webloader), putting on code isn't running in background

- (void)connectiondidfinishloading:(nsurlconnection *)connection {     dispatch_async(dispatch_get_global_queue( dispatch_queue_priority_default, 0), ^(void){     .......   }); 

which way better?

there problem. since part of tasks asynchronous, use completion blocks. not need run completion blocks in background, need figure out 1 last 1 finish, can run code clean , neatly package , ship data view controller.

the way thought of use bool each task, changing true when it's done. in completion blocks can check if other tasks complete, , if so, run clean code. however, may not elegant solution.

what best way deal these tasks, ensuring happens in background?

gcd groups used this. groups allow track arbitrary "members" of group, , hook block run when members of group have finished. it's quite handy. example (using code):

dispatch_async(dispatch_get_global_queue( dispatch_queue_priority_default, 0), ^(void){     dispatch_group_t group = dispatch_group_create();      dispatch_group_enter(group); // + 1     [self.webloader getfeedwithcompletion: ^{         self.thatothercompletionblock();         dispatch_group_leave(group); // - 1     }];      dispatch_group_enter(group); // + 1     [self.otherdataloader getdatawithcompletion:^{         self.completionblock();         dispatch_group_leave(group); // - 1     }];      dispatch_group_enter(group); // + 1     [self.thatdataloader getthatdatawithcompletion:^{         self.anothercompletionblock();         dispatch_group_leave(group); // - 1     }];      dispatch_group_notify(group, dispatch_get_global_queue( dispatch_queue_priority_default, 0), ^{         // executed once 3 of prior completion blocks have been run.         // i.e. when group "count" goes zero.     });      dispatch_release(group); }); 

you also, albeit bit circuitously, use nsoperation's inter-operation dependency feature achieve this. this:

nsoperationqueue* q = [[[nsoperationqueue alloc] init] autorelease];  nsoperation* completiona = [nsblockoperation blockoperationwithblock: self.thatothercompletionblock]; nsoperation* completionb = [nsblockoperation blockoperationwithblock: self.completionblock]; nsoperation* completionc = [nsblockoperation blockoperationwithblock: self.anothercompletionblock];  nsblockoperation* afterallthree = [[[nsblockoperation alloc] init] autorelease]; [afterallthree adddependency: completiona]; [afterallthree adddependency: completionb]; [afterallthree adddependency: completionc]; [afterallthree addexecutionblock:^{     // executed once 3 of prior completion blocks have been run. }];  // kick off tasks [q addoperationwithblock:^{     [self.webloader getfeedwithcompletion: ^{ [q addoperation: completiona];}];     [self.otherdataloader getdatawithcompletion:^{ [q addoperation: completionb]; }];     [self.thatdataloader getthatdatawithcompletion:^{ [q addoperation: completionc]; }]; }]; 

i prefer dispatch_group method, both job done.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -