Multiple environments
Read time: 7 minutes
Last edited: May 02, 2024
Overview
This topic explains how to support multiple environments in LaunchDarkly mobile SDKs.
A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users." To learn more, read Contexts.
Creating contexts and evaluating flags based on them is supported in the latest major versions of most of our SDKs. For these SDKs, the code samples on this page include the two most recent versions.
Client-side SDKs
Some LaunchDarkly client-side mobile SDKs support having multiple LDClient
instances tied to separate mobile keys. This allows the SDK to evaluate flags from multiple environments. The SDK can evaluate flags from different environments whether they are all in the same LaunchDarkly project or different LaunchDarkly projects.
However, other LaunchDarkly client-side mobile SDKs, including Android, iOS, and React Native, do not support multiple LDClient
instances. Instead, you can configure the one SDK instance to connect to multiple environments.
This feature is available in the following client-side SDKs:
Android
Expand Android code sample
All LDClient
instances evaluate against the same LDContext
. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig
object.
Here's how:
To access the secondary mobile key instances, use the getForMobileKey
method on LDClient. This method takes the identifier name assigned to your environment key in the secondaryMobileKeys
map and returns the associated LDClient instance. Track calls, listeners, and flag evaluation are all tied to the client instance they are evaluated against.
Here's how:
As all the client instances use the same LDClient object, some calls affect all instances.
These methods include:
To learn more, read LDClient
.
iOS
Expand iOS code sample
All LDClient
instances evaluate against the same LDContext
. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig
object.
To access the secondary mobile key instances, use the LDClient.get
static method on LDClient. This method takes the identifier name assigned to your environment key in the secondaryMobileKeys
map and returns the associated LDClient instance. Track calls, listeners, and flag evaluation are all tied to the client instance they are evaluated against.
Here's how:
As all the client instances use the same LDClient
object, some SDK functionality affects all instances.
These methods include:
To learn more, read LDClient
.
React Native
Expand React Native code sample
Starting with version 10 of the React Native SDK, if you need to support multiple environments in the same application, you can create multiple clients. We do not recommend using multiple environments within one application, because it quickly becomes complex and difficult to manage. If your setup requires it, here's how:
Expand for working with multiple environments in earlier versions
In previous versions of the React Native SDK, all LDClient
instances evaluate against the same LDContext
. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig
object.
Here's how:
To use functionality with a secondary environment, provide the name of the desired secondary environment as the environment
parameter value to many LDClient
methods. Some LDClient
methods do not have an optional environment
parameter because they act across all environments.
To use functionality with a secondary environment, provide the name of the desired secondary environment as the environment
parameter value to many LDClient
methods.
Here's how:
Some LDClient
methods do not have an optional environment
parameter because they act across all environments.
These methods include: