ios - 从delphi调用 objective-c 代码块

标签 ios delphi objective-c-blocks firemonkey

我正在尝试在我的 firemonkey 应用程序中进行后台提取。

到目前为止,我的 perfromFetchWithCompletionHandler 被调用并下载新信息。

当我完成获取并需要调用completionHandler代码块时,问题就出现了,应用程序挂起并且我没有得到任何异常(至少我可以阅读)

设置:

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); <<--Stops here
  //Next line of code never gets called.
  NSLog3(Format('performFetchWithCompletionHandler:done, done',[ FetchResult ]) );
end;

performFetchWithCompletionHandler man page

我尝试过使用函数指针类型的不同声明,但由于它并不是一个函数指针,我想这就是它不起作用的原因。

有什么想法吗?

谢谢 罗伯特

最佳答案

我找到了一个有效的解决方案:imp_implementationWithBlock

procedure performFetchWithCompletionHandler(self : id; _cmd : SEL; application:    PUIApplication; handler : id );

//将处理程序更改为类型 id(指针)

const
  libobjc = '/usr/lib/libobjc.dylib';

{$IF NOT DECLARED(_PU)}
  const
  {$IFDEF UNDERSCOREIMPORTNAME}
    _PU = '_';
  {$ELSE}
    _PU = '';  
  {$ENDIF}
  {$EXTERNALSYM _PU}
{$ENDIF}

function imp_implementationWithBlock( block :id ) : IMP; cdecl; external libobjc name  _PU + 'imp_implementationWithBlock';
function imp_removeBlock( anImp : IMP ) : integer; cdecl; external libobjc name _PU + 'imp_removeBlock';

//添加了对 libobjc 中的 imp_implementationWithBlock 和 imp_removeBlock 的引用

type  IMP = function( self : id; cmd : SEL; Param1 : NSUInteger ) : id; cdecl;

//将类型 IMP 声明为与 imp_implementationWithBlock 返回的函数指针相对应的 c 函数,在这种特殊情况下,带有一个 NSUInteger 参数。

procedure performFetchWithCompletionHandler(self : id; _cmd : SEL; application: PUIApplication; handler : id );
var
  ahandlerimp : IMP;
begin
   .... //Code to perform fetch 
  ahandlerimp := imp_implementationWithBlock( handler ); //Create c function for block
  ahandlerimp(self,_cmd,FetchResult ); //Call c function, _cmd is ignored
  imp_removeBlock(ahandlerimp); //Remove the c function created two lines up
end;

关于ios - 从delphi调用 objective-c 代码块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23130639/

相关文章:

ios - UIButton 和图像

Delphi抽象类避免实现

ios - 从 block 返回- objective-c

delphi - 为什么当 LineTo 成功时 FillRect 不绘制?

iOS:使用 NSNotificationCenter 在 userInfo 中发布代码块?

ios - dispatch_sync 在队列为空之前返回

ios - 静态库在 iOS 模拟器上出错并在 iOS 设备上工作

ios - 如何在单击按钮时在 UITableView 中添加单元格?

ios - 基于网络响应在表格 View 单元格中设置不同标签的多种字体

delphi - 如何安装 OmniXml for Delphi Xe2/OExport XLSX/ODS native Delphi/Lazarus 导入/导出库