Giter Site home page Giter Site logo

jonnybeegod / cloudkitfeaturetoggles Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 47 KB

Native CloudKit Feature-Toggles written in Swift

License: MIT License

Swift 100.00%
swift ios swift-package-manager cloudkit feature-toggles feature-toggle feature-switcher feature-switches

cloudkitfeaturetoggles's Introduction

CloudKit FeatureToggles

codecov Swift Package Manager iOS Twitter: @jonezdotcom

What does it do?

Feature Toggles offer a way to enable or disable certain features that are present in your codebase, switch environments or configurations or toggle between multiple implementations of a protocol - even in your live system at runtime. CloudKit FeatureToggles are implemented using CloudKit and are therefor associated with no run costs for the developer. Existing Feature Toggles can be changed in the CloudKit Dashboard and are delivered immediately via silent push notifications to your users.

How to install?

CloudKitFeatureToggles is compatible with Swift Package Manager. To install, simply add this repository URL to your swift packages as package dependency in Xcode. Alternatively, add this line to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/JonnyBeeGod/CloudKitFeatureToggles", from: "0.1.0")
]

And don't forget to add the dependency to your target(s).

How to use?

CloudKit Preparations

  1. If your application does not support CloudKit yet start with adding the CloudKit and remote background notification entitlements to your application
  2. Add a new custom record type 'FeatureStatus' with two fields:
Field Type
featureName String
isActive Int64

For each feature toggle you want to support in your application later add a new record in your CloudKit public database.

In your project

  1. In your AppDelegate, initialize a FeatureToggleApplicationService and hook its two UIApplicationDelegate methods into the AppDelegate lifecycle like so:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return featureToggleApplicationService.application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        featureToggleApplicationService.application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)
}

  1. Anywhere in your code you can create an instance of FeatureToggleUserDefaultsRepository and call retrieve to fetch the current status of a feature toggle.

โš ๏ธ Note that retrieve returns the locally saved status of your toggle, this command does not trigger a fetch from CloudKit. Feature Toggles are fetched from CloudKit once at app start from within the FeatureToggleApplicationService UIApplicationDelegate hook. Additionally you can subscribe to updates whenever there was a change to the feature toggles in CloudKit as shown in the next section.

  1. You have to call retrieve with your implementation of a FeatureToggleIdentifiable. What I think works well is creating an enum which implements FeatureToggleIdentifiable:
enum FeatureToggle: String, FeatureToggleIdentifiable {
        case feature1
        case feature2
        
        var identifier: String {
            return self.rawValue
        }
        
        var fallbackValue: Bool {
            switch self {
            case .feature1:
                return false
            case .feature2:
                return true
            }
        }
    }

Notifications

You can subscribe to updates from your feature toggles in CloudKit by subscribing to the onRecordsUpdated Notification like so:

NotificationCenter.default.addObserver(self, selector: #selector(updateToggleStatusFromNotification), name: NSNotification.Name.onRecordsUpdated, object: nil)
@objc
private func updateToggleStatusFromNotification(notification NSNotification) {
    guard let updatedToggles = notification.userInfo[Notification.featureToggleUserInfoKey] as? [FeatureToggle] else {
        return
    }
    
    // do something with the updated toggle like e.g. disabling UI elements 
}

Note that the updated Feature Toggles are attached to the notifications userInfo dictionary. When this notification has been sent the updated values are also already stored in the repository.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.