目录
目录README.md

ApolloCombine

GitHub release (latest SemVer) Swift Package Manager Compatible Cocoapods GitHub

A collection of Combine publishers for the Apollo iOS client.

Usage

The extension to ApolloClientProtocol (in the aptly named ApolloClientExtensions) includes methods whose inputs mirror the existing ApolloClientProtocol operation methods. Instead of including a result handler, though, these methods return Combine publishers that deliver the results of the operation to subscribers.

When cancel() is invoked on a subscription, the underlying Apollo operation is also cancelled.

fetchPublisher, performPublisher, uploadPublisher, and clearCachePublisher will send a completion when the operation has completed.

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 and subscribePublisher will not send a completion, instead delivering data and errors as the value of the subscription. This allows these operations to remain open after an error has been encountered. As such, it is important to explicitly cancel these subscriptions when you are done with them to avoid memory leaks.

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()

Installation

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. Use Xcode’s Swift Packages option, which is located within the File menu.

CocoaPods

ApolloCombine is available through CocoaPods. To install it, simply add the following line in your Podfile

pod 'ApolloCombine'

License

ApolloCombine is released under the MIT license. See LICENSE for details.

关于
57.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号