Swiftpack.co - crane-hiromu/CombineStorable as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by crane-hiromu.
crane-hiromu/CombineStorable 1.1.0
Handy Combine extensions on NSObject, including Set<AnyCancellable>.
⭐️ 22
🕓 2 years ago
.package(url: "https://github.com/crane-hiromu/CombineStorable.git", from: "1.1.0")

Storable

image

Description

If you're using Combine, you've probably encountered the following code more than a few times.

class Object: NSObject {
    var cancellables = Set<AnyCancellable>()

    ...
}

Instead of adding a new property to every object, use this library to add it for you, to any subclass of NSObject.

Example1

final class Object: NSObject, Storable { // import Storable

    // You don't have to write this.
    // private var cancellables = Set<AnyCancellable>()

    func exec() {
        Just<Int>(0)
            .sink(receiveCompletion: { _ in
                // do something
            }, receiveValue: { _ in 
                // do something
            })
            .store(in: &self.cancellables) // read Set<AnyCancellable> in yourself.
    }
}

You can read Set<AnyCancellable> without member variable. It'll work just like a property: when the instance is deinit'd, the Set<AnyCancellable> gets disposed. It's also a readwrite property, so you can use your own, too.


Furthermore, .store(in: &self.cancellables) can be omitted.

Example2

final class Object: NSObject, Storable {

    func exec() {
        Just<Int>(0)
            .sink(self, receiveCompletion: { _ in // input self
                // do something
            }, receiveValue: { _ in 
                // do something
            })
    }
}

For a detailed explanation, please read here.

Installing

CocoaPods

Add the following line to your Podfile:

pod 'CombineStorable', :git => '[email protected]:crane-hiromu/CombineStorable.git', :branch => 'main'

Carthage

Add the following to your Cartfile:

github "crane-hiromu/CombineStorable"

Supplement

If you fail to install, reset caches. (rm -rf ~/Library/Caches/)

Swift Package Manager

Add the following dependency to your Package.swift file:

.package(url: "https://github.com/crane-hiromu/CombineStorable.git", from: "1.1.0")

License

MIT, of course ;-) See the LICENSE file.

GitHub

link
Stars: 22
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

First Release
3 years ago

Changes 🆕

  • Release
  • Support for Cocoapods
  • Support for Carthage
  • Support for SPM

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