RxCombine



Build Status Code Coverage for RxCombine on codecov
RxCombine supports CocoaPods RxCombine supports Swift Package Manager (SPM) RxCombine supports Carthage

RxCombine 提供了 RxSwift 和 Apple 的 Combine 框架之间的双向类型桥接。

注意:这是一个高度实验性的项目,基本上只是一个快速拼凑的 PoC。 我很乐意接受 PR、想法、意见或改进。 谢谢! :)

基础示例

请查看 ExampleApp 文件夹中的示例 App。 在打开项目之前运行 pod install

安装

CocoaPods

将以下行添加到你的 Podfile

pod 'RxCombine'

Swift Package Manager

将以下依赖项添加到你的 Package.swift 文件

.package(url: "https://github.com/CombineCommunity/RxCombine.git", from: "1.6.0")

Carthage

Carthage 支持以预构建二进制文件的形式提供。

将以下内容添加到你的 Cartfile

github "CombineCommunity/RxCombine"

我想 ...

在我的 Combine 代码中使用 RxSwift

RxCombine 提供了几个助手和转换,以帮助你将现有的 RxSwift 类型桥接到 Combine。

注意:如果你想了解更多关于 Combine 中来自 RxSwift 的并行运算符,请查看我的 RxSwift to Combine Cheat Sheet (或在 GitHub 上)

let observable = Observable.just("Hello, Combine!")

observable
    .publisher // AnyPublisher<String, Swift.Error>
    .sink(receiveValue: { value in ... })
let relay = BehaviorRelay<Int>(value: 0)

// Use `sink` on RxSwift relay
let combineSubject = relay.toCombine()

combineSubject.sink(receiveValue: { value in ... })

// Use `send(value:)` on RxSwift relay
combineSubject.send(1)
combineSubject.send(2)
combineSubject.send(3)

在我的 RxSwift 代码中使用 Combine

RxCombine 提供了几个助手和转换,以帮助你将 Combine 代码和类型桥接到你现有的 RxSwift 代码库中。

// A publisher publishing numbers from 0 to 100.
let publisher = AnyPublisher<Int, Swift.Error> { subscriber in
    (0...100).forEach { _ = subscriber.receive($0) }
    subscriber.receive(completion: .finished)
}

publisher
    .asObservable() // Observable<Int>
    .subscribe(onNext: { num in ... })
// Combine Subject
let subject = PassthroughSubject<Int, Swift.Error>()

// A publisher publishing numbers from 0 to 100.
let publisher = AnyPublisher<Int, Swift.Error> { subscriber in
    (0...100).forEach { _ = subscriber.receive($0) }
    subscriber.receive(completion: .finished)
}

// Convert a Publisher to an Observable and bind it
// back to a Combine Subject 🤯🤯🤯
publisher.asObservable()
         .bind(to: subject)

Observable.of(10, 5, 7, 4, 1,  6)
          .subscribe(subject.asAnyObserver())

未来想法

许可证

MIT 许可证,当然 ;-) 请参阅 LICENSE 文件。

Apple 标志和 Combine 框架是 Apple Inc. 的财产。