RxAsyncAwait is an extension for using asynchronous functions on streams in RxSwift.
I don't think it's preferred, but if you need to use asynchronous functions in the RxSwift streams, you'll have to write some annoying code to handle it because RxSwift operators don't support easy use for asynchronous functions.
This library can keep your code simple even if that happens.
Observable.just("https://github.com/byoth")
.compactMap { URL(string: $0) }
.tryAwait { try await URLSession.shared.data(from: $0) }
.subscribe()
.disposed(by: disposeBag)
Observable.just("https://github.com/byoth")
.compactMap { URL(string: $0) }
.await { await getData(from: $0) }
.subscribe()
.disposed(by: disposeBag)
func getData(from url: URL) async -> (Data, URLResponse)? {
try? await URLSession.shared.data(from: url)
}
import PackageDescription
let package = Package(
name: "Your App",
dependencies: [
.package(url: "https://github.com/byoth/RxAsyncAwait.git", .upToNextMajor(from: "0.1.0")),
]
)
target 'Your Target' do
pod 'RxAsyncAwait', '~> 0.1'
end
- Xcode 13.2
- Swift 5.5
- RxSwift ~> 6.0.0