中文)
CXFoundation( Combine + Foundation
,基于CombineX构建。
DispatchQueue
JSONDecoder
JSONEncoder
NotificationCenter
OperationQueue
PropertyListDecoder
PropertylistEncoder
RunLoop
Timer
URLSession
通知
此库仍在测试中,因此请不要在生产环境中使用!
实时示例
计时器
let ping = Timer.cx.publish(every: 1, on: RunLoop.main, in: .default)
.sink { d in
// ping
}
URL会话
let request = URLSession.shared.cx.dataTaskPublisher(for: host)
.sink { (data, response) in
// update ui
}
通知中心
let observe = NotificationCenter.default.cx.publisher(for: name)
.sink { notification in
//
}
调度器
let cancel = upstream
.receive(on: RunLoop.main.cx.scheduler)
.sink { v in
//
}
安装
Swift 包管理器
dependencies.append(
.package(url: "https://github.com/cx-org/CXFoundation", .branch("master"))
)
CocoaPods
pod 'CxFoundation', :git => 'https://github.com/cx-org/CXFoundation.git', :branch => 'master'
Carthage
github "cx-org/CXFoundation" "master"
相关
常见问题解答
为什么不直接使用Combine?
见这里。
为什么接口与Combine不完全相同?
Combine
直接扩展系统类型。我们无法在相同位置定义相同的接口 - 它无法编译。例如
extension URLSession {
struct DataTaskPublisher: Publisher {
// ...
}
}
如果接口不一致,迁移是否会非常麻烦?
CXCompatible提供所有cx
接口的实现,但基于Combine
,无需CombineX
依赖。这可以帮助您轻松迁移。