objective c blocks - completionHandler definition inside handleEventsForBackgroundURLSession: -


this not trivial question asked here in stackoverflow before, @ least haven’t found similar, of course googled , read of high ranked results.

btw, if folks here don't feel comfortable objective c’s block syntax, visit page please http://fuckingblocksyntax.com ,
before throwing block related issues.

1st part of question is: the background of declaration of block-parameter, invoking method has block-parameter ( in many cases, completionblock )

the “callee-method" in myworker class: … ...

@implementation myworker -(void) aworkermethodneedsablockinput: ((void)(^)( nsobject *, double )) blockparam {        nsobject *anobj=[[ nsobject alloc] init];        double *adouble;        [self retrievetimeconsumingresults: anobj withnumberoftry: adouble ];        blockparam ( anobj, * adouble );   }  @end 

the “caller-method" in mymanager class:

@interface mymanager() @property (nonatomic) myworker * mworker; @property (nonatomic, copy)  (void)(^mblockproperty)( nsobject *, double ); @end @implementation mymanager -(void) amanagermethodwhocallsworkerwithcompletionhandler {     (void)(^ valblock )( nsobject *, double ) = ^(void)( nsobject * realobj, double realdouble )      {                  [realobj performselector:@sel( aselector) withobject: @(realdouble) afterdelay: atimeinterval];         } ;    self.mblockproperty=[valblock copy];    [self.mworker aworkermethodneedsablockinput : self.mblockproperty];  }  @end 

above sudo-code normal way, in our custom code, of storing block inside property, declaring block parameter , offering block’s arguments in callee; providing block definition , “consuming” block’s arguments in caller. keep 'void' returntype in writing clarity of block-syntax. correct writing if did wrong, please!

2nd part of question:

the routine usage of

    - (void)application:(uiapplication *)application handleeventsforbackgroundurlsession:(nsstring *)identifier completionhandler:(void (^)())completionhandler {     nslog(@"handle events background url session");      self.backgroundsessioncompletionhandler = completionhandler; } 

then later

- (void)urlsessiondidfinisheventsforbackgroundurlsession:(nsurlsession *)session {     webappdelegate *appdelegate = (webappdelegate *)[[uiapplication sharedapplication] delegate];     if (appdelegate.backgroundsessioncompletionhandler) {         void (^completionhandler)() = appdelegate.backgroundsessioncompletionhandler;         appdelegate.backgroundsessioncompletionhandler = nil;          completionhandler();     }     nslog(@"all tasks finished"); } 

the background callback via daemon works in above pattern based on nsurlsession framework, right? did many times, not problem on applying such pattern.

which have been wondering long time is:

what inside definition of completionhandler parameter of “handleeventsforbackgroundurlsession:” method, when method invoked block-property storage? < @ time when “ completionhandler();” executed > have never seen sample/demo put/copy block-of-code completionhandler... or wish know much?

what inside definition of completionhandler parameter of “handleeventsforbackgroundurlsession:” method, when method invoked block-property storage? < @ time when “ completionhandler();” executed > have never seen sample/demo put/copy block-of-code completionhandler... or wish know much?

if understand question correctly, asking implementation inside block passed application's implementation of uiapplicationdelegate method application:handleeventsforbackgroundurlsession:completionhandler: system.

application:handleeventsforbackgroundurlsession:completionhandler: invoked (indirectly) external service process. when application uses nsurlsession create background session, session managed system service. service actual background transfer , notifies uikit/foundation , in turn application through mechanism called xpc. xpc used macos developers, @ time not directly available ios applications - many of apis , services used developers on ios communicating xpc services.

in case of application:handleeventsforbackgroundurlsession:completionhandler:, block passed completionhandler parameter opaque callback. background transfer service needs know when application done handling events session. invoking block informs service application has processing of set of events , daemon can move on.

the block created , owned system , such application should not attempt modify or change (other copying block, right thing do!). applications should not provide own completion blocks - developer-provided block have no way inform transfer service of completion unless wrapped block passed completionhandler: itself.

the background transfer service , nsurlsession introduced in ios 7. if writing third party framework or library can beneficial take advantage of service, framework must provide way handle events background session owns. perhaps because of few third party libraries seem support background transfers. supporting not difficult - library needs method indicate ownership of session, , method take completion block , handle events:

- (void)application:(uiapplication *)application handleeventsforbackgroundurlsession:(nsstring *)identifier completionhandler:(void (^)())completionhandler {      if ([somecloudframeworkobject canhandleeventsforsessionwithidentifier:identifier]){         [somecloudframeworkobject handleeventsforbackroundsessionwithidentifier:identifier completionhandler:completionhandler];     } } 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -