Resyncer

1.0.0

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

What's New

Initial release

2024-06-16T16:07:45Z

Initial release with basic support to callback based asynchronous code and swift-concurrency based asynchronous code

Resyncer

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

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

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 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.

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

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

Installation

Cocoapods

Add the dependency to the Resyncer framework in your Podfile:

pod 'Resyncer', '~> 1.0.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.0.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.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Sep 06 2024 14:14:29 GMT-0900 (Hawaii-Aleutian Daylight Time)