I'm trying to make background fetch work in my firemonkey application.
I've gotten so far that my perfromFetchWithCompletionHandler gets called and downloads new information.
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)
Setup:
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
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.
Any ideas?
Thanks Robert
This is what Apple calls "blocks".
You can find a bit more technical detail about them in the following document from Apple: http://www.opensource.apple.com/source/libclosure/libclosure-53/BlockImplementation.txt
Unfortunately it doesn't appear to be easy to implement that from Delphi. So it might be a better choice to create an objective-c dylib that will in turn take care of the block and use that from your Delphi application.
I've found a solution that works: imp_implementationWithBlock
//Changed the handler to type id (Pointer)
//Added references to imp_implementationWithBlock and imp_removeBlock from libobjc
//Declared type IMP as a c function corresponding to the function pointer returned by imp_implementationWithBlock in this particular case with one NSUInteger parameter.