OpenCombineAsync
'is an encapsulation based on OpenCombine Future
. It encapsulates Future
and provides async
and await
API to handle asynchronous events gracefully.
If your project is based on the system Combine
framework, and the project environment is macOS 10.15 + / iOS 10.0 + / tvOS 10.0 + / watchOS 6.0 +
, then you can choose to use [CombineAsync](https://raw.github.com/KKLater/OpenCombineAsync/main/ https://github.com/KKLater/CombineAsync ) framework. If your project uses the CombineX framework, you can choose CombineXAsync framework.
Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.
Xcode 11+ is required to build
OpenCombineAsync
using Swift Package Manager.
To integrate OpenCombineAsync
into your Xcode project using Swift Package Manager, add it to the dependencies value of your Package.swift
:
dependencies: [
.package(url: "https://github.com/KKLater/CombineXAsync.git", .upToNextMajor(from: "0.0.1"))
]
Asynchronous operations need to be wrapped as Future
objects of OpenCombine
.
func background1() -> Future<Int, Error> {
return Future<Int, Error> { promise in
let i:Int = Int(arc4random() % 10)
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.microseconds(i)) {
promise(.success(1))
}
}
}
func background2(c: Int) -> Future<Int, Error> {
return Future<Int, Error> { promise in
let i:Int = Int(arc4random() % 10)
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.microseconds(i)) {
promise(.success(c + 10))
}
}
}
func background3(c: Int) -> Future<Int, Error> {
return Future<Int, Error> { promise in
let i:Int = Int(arc4random() % 10)
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.microseconds(i)) {
promise(.success(c + 100))
}
}
}
Before use async
and await
api:
self.background1().sink { (error) in
print(error)
} receiveValue: { (a) in
self.background2(c: a).sink { (error) in
print(error)
} receiveValue: { (b) in
self.background3(c: b).sink { (error) in
print(error)
} receiveValue: { (c) in
print(c) // 111
}.store(in: &self.cancels)
}.store(in: &self.cancels)
}.store(in: &cancels)
After using the async
and await
APIs:
async {
do {
let a = try await(self.background1())
let b = try await(self.background2(c: a))
let c = try await(self.background3(c: b))
main {
print(c) // 111
}
} catch {
throw error
}
}
OpenCombineAsync
is released under the MIT
license. See LICENSE
for details.
link |
Stars: 0 |
Last commit: 3 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics