Open liamappelbe opened 1 month ago
@liamappelbe Can you point me at an example of this type of API?
The one Brian ran into in cupertino_http was URLSession:downloadTask:didFinishDownloadingToURL:. It's called after a download to a temporary file is complete. The temporary file will be deleted after the callback returns. So if you want to do something with the file, your callback has to be blocking.
@brianquinlan What were the other examples you found that were using the completion handler pattern, but expect the completion handler to be called during the callback?
With a little more infrastructure, it would also be possible to support callbacks that return results, but these are even rarer in Apple APIs, so we'll wait until we have a use case for this.
We're auto-converting all delegate protocols to callbacks still, right? If so, this is not going to be rare. Having a delegate to provide data or synchronous decisions is fairly common pattern.
@brianquinlan What were the other examples you found that were using the completion handler pattern, but expect the completion handler to be called during the callback?
URLSession:task:didReceiveChallenge:completionHandler:
=>
"A handler that your delegate method must call"application:handleIntent:completionHandler:
=>
"The handler block to execute with your response. You must execute this handler at some point during your implementation of this method."I didn't look very hard for examples.
Turns out there are a few Apple APIs that require that the completion handler is invoked during the callback. So we should add a variant of listener blocks and protocol methods that uses a mutex to block until the listener callback is complete.
NativeCallable.isolateLocal
style).With a little more infrastructure, it would also be possible to support callbacks that return results, but these are even rarer in Apple APIs, so we'll wait until we have a use case for this.