A swift library to make use of asynchronous API in a synchronous environment.
- Compatible with iOS and macOS
- No deadlocks
- Support for callback based asynchronous code
- Support for swift-concurrency based asynchronous code
Resyncer help you calling asynchronous code in a synchronous environment by suspending current thread execution, waiting for asynchronous work to complete. This is done by offloading asynchronous work to a different thread either by using an OperationQueue or by making use of swift-concurrency Task.
Because Resyncer is going to block the calling thread, make sure not to use it from the Main Thread.
If you have an asynchronous function that posts a value on a provided callback using swift Result
(or also without Result
, you can construct it yourself):
func asyncWork(_ completion: @escaping (Result<Int, Error>) -> Void) { ... }
You can use Resyncher to obtain the produced value in a synchronouse environment:
let x = try resyncer.synchronize { callback in
self.asyncWork { result in
callback(result)
}
}
If you have an asynchronous function that returns a value:
func asyncWork() async throws -> Int { ... }
You can use Resyncher to obtain the produced value in a synchronouse environment:
let x = try resyncer.synchronize { callback in
try await self.asyncWork()
}
Add the dependency to the Resyncer
framework in your Podfile
:
pod 'Resyncer', '~> 1.0.0'
Add it as a dependency in a Swift Package:
dependencies: [
.package(url: "https://github.com/danielepantaleone/Resyncer.git", .upToNextMajor(from: "1.0.0"))
]
If you like this project you can contribute it by:
- Submit a bug report by opening an issue
- Submit code by opening a pull request
MIT License
Copyright (c) 2024 Daniele Pantaleone
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.