Swiftpack.co -  crane-hiromu/CombineStorable as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
crane-hiromu/CombineStorable
Handy Combine extensions on NSObject, including Set.
.package(url: "https://github.com/crane-hiromu/CombineStorable.git", from: "1.0.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 => 'git@github.com:crane-hiromu/CombineStorable.git', :branch => 'main'

Carthage

Add the following to your Cartfile:

github "crane-hiromu/CombineStorable"

Swift Package Manager

Add the following dependency to your Package.swift file:

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

License

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

GitHub

link
Stars: 10
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.

Release Notes

First Release
2 weeks ago

Changes 🆕

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

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