一个为 Apollo iOS 客户端 提供的 Combine 发布者集合。
ApolloClientProtocol 的扩展(位于名为 ApolloClientExtensions
的文件中)包含的方法,其输入与现有的 ApolloClientProtocol 操作方法相对应。不同的是,这些方法不包含结果处理程序,而是返回 Combine 发布者,这些发布者将操作结果传递给订阅者。
当在订阅上调用 cancel()
时,底层 Apollo 操作也会被取消。
fetchPublisher
、performPublisher
、uploadPublisher
和 clearCachePublisher
将在操作完成后发送完成信号。
import ApolloCombine
let client = ApolloClient(...)
let fetchSubscription = client.fetchPublisher(query: MyQuery(), cachePolicy: .fetchIgnoringCacheData)
.sink(receiveCompletion: { completion in
// handle .finished or .failure
}, receiveValue: { graphQLResult in
// handle returned fetch data
})
// Cancelling the Combine subscription will also cancel the underlying Apollo operation
fetchSubscription.cancel()
watchPublisher
和 subscribePublisher
不会发送完成信号,而是将数据和错误作为订阅的值传递。这使得这些操作在遇到错误后仍能保持打开状态。因此,在完成这些订阅后,务必显式地取消它们,以避免内存泄漏。
import ApolloCombine
let client = ApolloClient(...)
let watchSubscription = client.watchPublisher(query: MyQuery())
.sink(receiveValue: { operationResult in
switch operationResult {
case .success(let graphQLResult):
// handle returned data
case .failure(let error):
// handle error
})
// Don't forget to cancel when you're done
watchSubscription.cancel()
Swift Package Manager 是一种用于自动化 Swift 代码分发的工具,并已集成到 swift
编译器中。使用 Xcode 的 Swift Packages 选项,该选项位于 File 菜单中。
ApolloCombine 可以通过 CocoaPods 获得。要安装它,只需在您的 Podfile
中添加以下行
pod 'ApolloCombine'
ApolloCombine 在 MIT 许可证下发布。 查看 LICENSE 了解详情。