The Powerful use of Feature Flag with Combine Swift
I. Feature Flag
Concept
Features flags are a popular development technique that allows you to turn certain functionality on and off during runtime. This will not require new code deployment and will allow you better control and more experimentation of both new and old features.
Benefits
- Testing gradually
You will be able to release a new feature gradually in production. With metrics feedback you will be able to track if your feature is reliable and should be deploy to all your users.
- A/B Testing
You can implement A/B testing using feature flag by enabling gradually a feature to certain segmentation of users. With good metrics analysis, you’ll be able to track what’s the best feature to propose to your users
- Kill Switch
Enabling or disabling a feature will be as simple a turn on/off a button.
Why mix it with Combine ?
Mixing Combine framework with feature flags will help you to asynchronously enable or disable any feature without effort.
II. Combine Swift
The Combine framework provides a declarative Swift API for processing values over time. These values can represent many kinds of asynchronous events.
The Combine framework can be compared to RxSwift or ReactiveSwift frameworks.
Combine works with Publishers and subscribers. A Publisher exposes values that can change on which a subscriber subscribes to receive all those updates.
Development process
Implementing feature flags in your code should not be a complicated process. In our use case scenario, we’ll use Firebase Remote Config to implement the feature flags.
III. Use Case scenario
Headlines
In this scenario, we are developing an express checkout for an e-commerce application.
We will be able to release the app with remotely control of the feature .
Some code
First of all we need to implement Firebase Remote Config tool. For more details informations about it, I will let you consult Firebase documentation.
RemoteConfig Extension
We create a way to transform the remote config response to AnyPublisher
Feature Flag Helper
Then we create a FeatureFlag Helper to retrieve our configuration from Firebase. We also make sure to make it generic to help us to mock up for unit testing.
DetailsProduct View Model
We create a view model for our Product Details Page, and implement a load() function that will help us to retrieve our configuration and know if the express checkout is available.
BuyBox View
In our Buy box View we add a condition to check if the availability of the feature.
IV. Conclusion : Firebase Kill Switch
Now that we had configure all the environment we can safely release our application and monitor the feature with Firebase.
If we discover any sort of problem we’ll be able to safely shut-down the feature from Firebase Interface by setting the value to false.
Good reads:
- Introducing Combine
- Combine in practice
- Problem Solving with Combine Swift
- Handling Loading State within SwiftUI