HomeiOS Developmentgoal c - The right way to accurately name an iOS completionHandler...

goal c – The right way to accurately name an iOS completionHandler block from Delphi with out crashing?


I’m attempting to implement this iOS delegate methodology in Delphi:

- (void)URLSession:(NSURLSession *)session
              process:(NSURLSessionTask *)process willBeginDelayedRequest:(NSURLRequest *)request   completionHandler:(void (NS_SWIFT_SENDABLE ^)(NSURLSessionDelayedRequestDisposition disposition,
                                                NSURLRequest * _Nullable newRequest))completionHandler API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));

My Delphi translation appears like this:

process TURLSessionDelegate.URLSession(session: NSURLSession;
                                         process: NSURLSessionTask;
                                         willBeginDelayedRequest: NSURLRequest;
                                         completionHandler: Pointer);
var
  LCompletionHandlerBlock: process(disposition: NSURLSessionDelayedRequestDisposition;
                                     newRequest: NSURLRequest); cdecl;
start
  @LCompletionHandlerBlock := imp_implementationWithBlock(completionHandler);
  attempt
    LCompletionHandlerBlock(NSURLSessionDelayedRequestCancel, nil);
  lastly
    imp_removeBlock(@LCompletionHandlerBlock);
  finish;
finish;

the issue: it crashes on the decision

LCompletionHandlerBlock(NSURLSessionDelayedRequestCancel, nil);

Nevertheless, this model appears to work:

process TALHttpWorker.TURLSessionDelegate.URLSession(session: NSURLSession;
                                                       process: NSURLSessionTask;
                                                       willBeginDelayedRequest: NSURLRequest;
                                                       completionHandler: Pointer);
var
  LCompletionHandlerBlock: process(disposition: NSURLSessionDelayedRequestDisposition;
                                     zzz: Pointer;
                                     newRequest: Pointer;
                                     www: Pointer); cdecl;
start
  @LCompletionHandlerBlock := imp_implementationWithBlock(completionHandler);
  attempt
    LCompletionHandlerBlock(NSURLSessionDelayedRequestUseNewRequest,
                            nil,
                            NSObjectToID(willBeginDelayedRequest),
                            nil);
  lastly
    imp_removeBlock(@LCompletionHandlerBlock);
  finish;
finish;

If I declare the block as process(disposition: NSURLSessionDelayedRequestDisposition; newRequest: NSURLRequest); cdecl; → it crashes when invoked.

If I declare it as process(disposition: NSURLSessionDelayedRequestDisposition; zzz: Pointer; newRequest: Pointer; www: Pointer); cdecl; and go dummy nil values for zzz and www → it really works, and disposition / newRequest are accurately taken under consideration.

I don’t perceive why I want these two additional dummy parameters to make it work.
The unique Goal-C block signature solely has two parameters:

void (^)(NSURLSessionDelayedRequestDisposition disposition,
         NSURLRequest * _Nullable newRequest)
  • Why does the “pure” Delphi translation with solely two parameters crash?
  • Why does including two additional Pointer parameters (zzz / www) all of the sudden make it work?
  • What’s the appropriate Delphi declaration for this completionHandler block when utilizing imp_implementationWithBlock?

Any rationalization in regards to the calling conference / block ABI right here (and correctly map it in Delphi) can be very useful.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments