Swiftpack.co - adamaszhu/iCombine as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
adamaszhu/iCombine
Support combine syntax on iOS 9 and above
.package(url: "https://github.com/adamaszhu/iCombine.git", from: "0.4.8")

iCombine



iCombine gives developers the ability to use Combine syntax on iOS below 13. In other word, iCombine makes Combine available on iOS 12 to 8. The way that we do this is to create a wrapper around RxSwift to offer Combine syntax.

Q: What's the benefit of iCombine?

A: Developers can write Combine code now for their applications to support all the iOS from 15 to 8 and when the time is right, they can do a global find and replace to replace all the import iCombine by import Combine and everything will just work.

Install

Cocoapods

Include follow pod line in the Podfile

pod iCombine

Swift package manager

Add a Package Dependency using the current Github URL

How to use

import iCombine

let subscriber = Subscribers.Sink<Int, Never>(
    receiveCompletion: { completion in
        print(completion)
    }) { value in
        print(value)
    }
Publishers
      .Sequence<[Int], Never>(sequence: [1, 2, 3, 4])
      .receive(subscriber: subscriber)

Documentation

Combine Interfaces

Snippet

To bridge iCombine with ReactiveSwift, please use this snippet iCombine+Convertable.swift

For ReactiveSwift 5.x please use this snippet iCombine+ReactiveSwift5.swift

WIP

Publisher

  • ☑ receive<S>(subscriber: S)
  • ☑ eraseToAnyPublisher

AnyPublisher

  • ☑ init<P>(_ publisher: P)

Just

  • ☑ init

HandleEvents

  • ☑ init
  • ☑ handleEvents

Map

  • ☑ map
  • ☑ tryMap

MapError

  • ☑ init(upstream: Upstream, transform: @escaping (Upstream.Failure) -> Failure)
  • ☑ init(upstream: Upstream, _ map: @escaping (Upstream.Failure) -> Failure)
  • ☑ mapError

Publishers

TryMap

  • ☑ map
  • ☑ tryMap

Map

  • ☑ init(upstream: Upstream, transform: @escaping (Upstream.Output) -> Output)
  • ☑ map
  • ☑ tryMap

Sequence

  • ☑ init

  • ☑ map

  • ☑ append(_ publisher: Publishers.Sequence<Elements, Failure>)

  • ☑ append<S>(_ elements: S)

  • ☑ append(_ elements: Publishers.Sequence<Elements, Failure>.Output...)

  • ☑ prepend(_ publisher: Publishers.Sequence<Elements, Failure>)

  • ☑ prepend<S>(_ elements: S)

  • ☑ prepend(_ elements: Publishers.Sequence<Elements, Failure>.Output...)

  • ☐ allSatisfy

  • ☐ tryAllSatisfy

  • ☐ collect

  • ☐ compactMap

  • ☐ contains

  • ☐ tryContains

  • ☐ drop

  • ☐ dropFirst

  • ☐ filter

  • ☐ ignoreOutput

  • ☐ prefix(_ maxLength: Int)

  • ☐ prefix(while predicate: (Elements.Element) -> Bool)

  • ☐ reduce

  • ☐ tryReduce

  • ☐ replaceNil

  • ☐ scan

  • ☐ setFailureType

Subject

  • ☑ behaviourSubject
  • ☐ send(_ value: Self.Output)
  • ☐ send(completion: Subscribers.Completion<Self.Failure>)
  • ☐ send(subscription: Subscription)

CurrentValueSubject

  • ☑ init
  • ☐ send(subscription: Subscription)
  • ☐ send(_ input: Output)
  • ☐ send(completion: Subscribers.Completion)

Subscribers

Completion

  • ☑ finished
  • ☑ failure(Failure)

Subscriber

  • ☐ receive(subscription: Subscription)
  • ☐ receive(_ input: Self.Input)
  • ☐ receive(completion: Subscribers.Completion<Self.Failure>)

Sink

  • ☑ init
  • ☐ disposable
  • ☐ receiveValue
  • ☐ receiveCompletion
  • ☐ receive(subscription: Subscription)
  • ☐ receive(_ value: Input) -> Subscribers.Demand
  • ☐ receive(completion: Subscribers.Completion)
  • ☐ cancel

Cancellable

  • ☑ cancel

Subscription

  • ☑ request

And more ...

RxSwift Combine Notes
ConnectableObservableType ConnectablePublisher
Driver BindableObject(SwiftUI) Both guarantee no failure, but Driver guarantees delivery on Main Thread. In Combine, SwiftUI recreates the entire view hierarchy on the Main Thread, instead.
Single Future They're only similar in the sense of single emission, but Future shares resources and executes immediately (very strange behavior)
bind(to:) assign(to: on:) Assign uses a KeyPath which is really nice and useful. RxSwift needs a Binder / ObserverType to bind.
buffer buffer
combineLatest combineLatest, tryCombineLatest
compactMap compactMap, tryCompactMap
debounce debounce
deferred Publishers.Deferred
delay delay
elementAt output(at:)
flatMapLatest switchToLatest
ignoreElements ignoreOutput
merge merge, tryMerge
merge(maxConcurrent:) flatMap(maxPublishers:)
multicast multicast
publish makeConnectable
reduce reduce, tryReduce
refCount autoconnect
scan scan, tryScan
share share There’s no replay or scope in Combine. Could be “faked” with multicast.
startWith prepend
subscribeOn subscribe(on:) RxSwift uses Schedulers. Combine uses RunLoop, DispatchQueue, and OperationQueue.
takeUntil prefix(untilOutputFrom:)
throttle throttle
timeout timeout
timer Timer.publish
toArray() collect()
window collect(Publishers.TimeGroupingStrategy) Combine has a TimeGroupingStrategy.byTimeOrCount that could be used as a window.

GitHub

link
Stars: 2
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics