objective c - How to use dispatch queue to run function -


i'm trying figure out how database fetch run in background. below foreground , background version of same function. foreground version works. in background version local variable retval never gets assigned. putting breakpoint in pageinfoforpagekey function tells me function never called.

is self available inside block?

//foreground version - (pageinfo*)objectatindex:(nsinteger)idx {     return [[self datacontroller] pageinfoforpagekey:[[[self pageids] objectatindex:idx] integervalue]]; }  //background version - (pageinfo*)objectatindex:(nsinteger)idx {     __block pageinfo* retval = nil;     __block nsinteger pageid = [[[self pageids] objectatindex:idx] integervalue];      dispatch_queue_t aqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);     dispatch_async(aqueue, ^{         retval = [[self datacontroller] pageinfoforpagekey:pageid];     });      return retval; } 

by using dispatch_async, telling system want run block time soon, , you don't want wait block finish (or start) before dispatch_async returns. definition of asynchronous. definition of “in background”.

the system doing told to: arranging block run, , returning immediately, before block has run. block doesn't set retval before return retval, because block hasn't run yet.

if want run database fetch in background, need change api pass retval (to whoever needs it) @ later time, after block has run. 1 way pass completion block message argument. common pattern performing fetches in background. example, @ +[nsurlconnection sendasynchronousrequest:queue:completionhandler:].

you might this:

- (void)fetchobjectatindex:(nsindex)idx completion:(void (^)(pageinfo *))block {     block = [block copy]; // unnecessary harmless if using arc     dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);     dispatch_async(queue, ^{         nsinteger pagekey = [[[self pageids] objectatindex:idx] integervalue];         pageinfo* pageinfo = [[self datacontroller] pageinfoforpagekey:pagekey];         dispatch_async(dispatch_get_main_queue(), ^{             block(pageinfo);         });     }); } 

then need change caller of objectatindex: use new api instead.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -