It will take about 3 minutes to finish reading this article.
Continuation is a mechanism used in Swift to handle asynchronous programming that helps developers write and manage asynchronous code more easily. In asynchronous programming, developers usually need to handle the results of asynchronous operations through callbacks, delegates, closures and other mechanisms. This can lead to poor code readability, difficult to manage and maintain, and is prone to Callback Hell. Continuations aims to solve these problems and provide a cleaner way of asynchronous programming.
Continuation provides two functions, withUnsafeContinuation and withUnsafeThrowingContinuation, that allow callback-based APIs to be invoked from within the asynchronous code.Each of these functions receives an operation closure that will invoke the callback-based API.The closure receives a Continuation instance, which must be recovered by the callback to provide either the result value or (in the Throwing variant) the thrown error, which becomes the result of the withUnsafeContinuation call when the asynchronous task is resumed.
Sample code one:
1 | class CheckedContinuationBootcampNetworkManager { |
The calling Code is as follows:
1 | let networkManager = CheckedContinuationBootcampNetworkManager() |
The Code that using the Continuation is as follows:
1 | class CheckedContinuationBootcampNetworkManager { |
The calling code is as follows:
1 | self.image = await networkManager.getHeartImageFromDatabase() |
The Code below looks more cleaner.
Sample code two:
1 | func getData(url: URL) async throws -> Data { |
After the optimization of the ‘withCheckedThrowingContinuation’:
1 | func getData2(url: URL) async throws -> Data { |
Note:
In the continuation callback, ‘resume’ must be guaranteed once.
Reference
[1] https://github.com/apple/swift-evolution/blob/main/proposals/0300-continuation.md
[2] https://www.youtube.com/watch?v=Tw_WLMIfEPQ&list=PLwvDm4Vfkdphr2Dl4sY4rS9PLzPdyi8PM&index=8