Skip to content

A swift library to make use of asynchronous API in a synchronous environment

License

Notifications You must be signed in to change notification settings

danielepantaleone/Resyncer

Repository files navigation

Resyncer

Cocoapods GitHub release (latest SemVer) GitHub GitHub Workflow Status (with event)

Resyncer is a Swift library designed to seamlessly integrate asynchronous APIs within synchronous environments. It allows developers to call asynchronous code—whether using callbacks or Swift’s async/await pattern—and block the calling thread until the asynchronous task is complete. This ensures that a result is available before proceeding with the synchronous workflow, making it ideal for scenarios where sequential execution is critical, but asynchronous tasks are involved.

Table of contents

Feature Highlights

  • Compatible with iOS and macOS
  • No deadlocks
  • Support for callback based asynchronous code
  • Support for swift-concurrency based asynchronous code

Basic usage

Resyncer enables you to call asynchronous code within a synchronous environment by pausing the current thread until the asynchronous task completes. It achieves this by offloading the asynchronous work to a separate thread, either using an OperationQueue or leveraging Swift concurrency with Task.

Because Resyncer is going to block the calling thread, make sure not to use it from the Main Thread.

Usage with callback based asynchronous code

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 synchronous environment:

let x = try resyncer.synchronize { callback in
    self.asyncWork { result in
        callback(result)
    }
}

Usage with swift-concurrency based asynchronous code

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 synchronous environment:

let x = try resyncer.synchronize {
    try await self.asyncWork()
}

Installation

Cocoapods

Add the dependency to the Resyncer framework in your Podfile:

pod 'Resyncer', '~> 1.1.0'

Swift Package Manager

Add it as a dependency in a Swift Package:

dependencies: [
    .package(url: "https://github.com/danielepantaleone/Resyncer.git", .upToNextMajor(from: "1.1.0"))
]

Contributing

If you like this project you can contribute it by:

License

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.

About

A swift library to make use of asynchronous API in a synchronous environment

Resources

License

Stars

Watchers

Forks

Packages

No packages published