Combine 操作符

  • AnyPublisher、AnySubscriber、AnySubject

通用类型,任意的 Publisher、Subscriber、Subject 都可以通过 eraseToAnyPublisher()、eraseToAnySubscriber()、eraceToAnySubject() 转化为对应的通用类型。

let name = Publishers.Sequence<[String], Never>(sequence: ["1","2"]).eraseToAnyPublisher()
  • Cancellable

可以取消活动或操作的协议。

public protocol Cancellable {

    /// Cancel the activity.
    /// Calling `cancel()` frees up any allocated resources. It also stops side effects such as timers, network access, or disk I/O.
 func cancel() }
  • Operator

操作符是 Combine 中非常重要的一部分,通过各式各样的操作符,可以将原来各自不相关的逻辑变成一致的(unified)、声明式的(declarative)的数据流。

转换操作符:

  • map/mapError
  • flatMap
  • replaceNil
  • scan
  • setFailureType

过滤操作符:

  • filter

  • compactMap

  • removeDuplicates

  • replaceEmpty/replaceError

  • reduce 操作符:

  • collect

  • ignoreOutput

  • reduce

运算操作符:

  • count
  • min/max

匹配操作符:

  • contains
  • allSatisfy

序列操作符:

  • drop/dropFirst
  • append/prepend
  • prefix/first/last/output

组合操作符:

  • combineLatest
  • merge
  • zip

错误处理操作符:

  • assertNoFailure
  • catch
  • retry

时间控制操作符:

  • measureTimeInterval
  • debounce
  • delay
  • throttle
  • timeout

其他操作符:

  • encode/decode
  • switchToLatest
  • share
  • breakpoint/breakpointOnError
  • handleEvents

猜你喜欢

转载自www.cnblogs.com/liuxiaokun/p/12684159.html