title: Calling objective C code block from delphi
body: <p>I'm trying to make background fetch work in my firemonkey application.</p>
  
  <p>I've gotten so far that my perfromFetchWithCompletionHandler gets called and downloads new information. </p>
  
  <p>The problem comes when I'm done with my fetch and need to call the completionHandler code block, the app hangs and I don't get any exception (that I can read at least)</p>
  
  <p>Setup:</p>
  
  <pre><code>TBackgroundFetchResultHandlerC = procedure ( AResult : NSUInteger ); cdecl;
  .. 
  ..
  
  procedure performFetchWithCompletionHandler(self : id; _cmd : SEL; application: PUIApplication; handler : TBackgroundFetchResultHandlerC );
  ..
  ..
  
  objc_msgSend((UIApp as ILocalObject).GetObjectId,
               sel_getUid('setMinimumBackgroundFetchInterval:'),
               Interval);
  class_addMethod( objc_getClass('DelphiAppDelegate') ,
                  sel_getUid('application:performFetchWithCompletionHandler:'),
                  @performFetchWithCompletionHandler,
                  'v@:@?'
    );
  ..
  ..
  
  procedure performFetchWithCompletionHandler(self : id; _cmd : SEL; application:         PUIApplication; handler : TBackgroundFetchResultHandlerC );
  var t : TThread;
  begin
    NSLog3('performFetchWithCompletionHandler:begin');
    Handler(0); &lt;&lt;--Stops here
    //Next line of code never gets called.
    NSLog3(Format('performFetchWithCompletionHandler:done, done',[ FetchResult ]) );
  end;
  </code></pre>
  
  <p><a href="https://developer.apple.com/library/ios/documentation/uikit/reference/uiapplicationdelegate_protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application%3aperformFetchWithCompletionHandler%3a" rel="nofollow">performFetchWithCompletionHandler man page</a></p>
  
  <p>I've tried with different declarations of the function pointer type, but as it isn't a function pointer exactly I guess that's why it won't work.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks 
  Robert</p>