Swiftpack.co - manmal/CombineLifetime as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
manmal/CombineLifetime
For when Set<AnyCancellable> is too limiting
.package(url: "https://github.com/manmal/CombineLifetime.git", from: "1.0.0")

CombineLifetime

When subscribing to a Swift Combine Publisher, we use a Set<AnyCancellable> most of the time, like this:

publisher
  .sink { value in
    // Do something with value
  }
  .store(in: &cancellables)

The problem with this is that Set is a value type, so it cannot be mutated if the owner is immutable. It is painful to use when the Set<AnyCancellable> needs to be stored in a struct or enum.

The solution for this is Lifetime, heavily inspired by ReactiveSwift's implementation. It's a final class, wraps a Set<AnyCancellable>, conforms to Cancellable and Hashable, and offers a convenient += operator:

lifetime += publisher
  .sink { value in
    // Do something with value
  }

Like any other Cancellable, it cancels the work automatically when it is deallocated, so make sure to keep its instance around.

Contact

🐦 Contact me via Twitter @manuelmaly

1.0.0

  • Added initial implementation and tests

GitHub

link
Stars: 0
Last commit: 1 week 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