• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""
EXPAND ALL

EDIT ON GITHUB

Multiple environments

Read time: 1 minute
Last edited: May 19, 2022

Overview

This topic explains how to support multiple environments in LaunchDarkly mobile SDKs.

Client-side SDKs

Some LaunchDarkly client-side SDKs support having multiple LDClient instances tied to separate mobile keys. This allows the SDK to evaluate flags from multiple environments.

This feature is available in the following client-side SDKs:

  • Android
  • iOS
  • React Native

Android

Expand Android code sample

All LDClient instances evaluate against the same LDUser. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig object.

Map<String, String> otherKeys = new HashMap<String, String>();
otherKeys.put("platform", "PLATFORM_MOBILE_KEY");
otherKeys.put("core", "CORE_MOBILE_KEY");
LDConfig ldConfig = new LDConfig.Builder()
.mobileKey("MOBILE_KEY")
.secondaryMobileKeys(otherKeys)
.build();
LDUser user = new LDUser.Builder("user key")
.email("fake@example.com")
.build();
LDClient.init(this.getApplication(), ldConfig, user);

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.

LDClient coreInstance = LDClient.getForMobileKey("core");
coreInstance.boolVariation("core-flag", false);

As all the client instances use the same LDUser object, some calls affect all instances.

LDClient coreInstance = LDClient.getForMobileKey("core");
// Calls affect all LDClient Instances
coreInstance.identify(/*User Object*/);
coreInstance.flush();
coreInstance.setOffline();
coreInstance.setOnline();
coreInstance.close();

Track calls, listeners, and flag evaluation are all tied to the client instance they are evaluated against.

iOS

Expand iOS code sample

All LDClient instances evaluate against the same LDUser. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig object.

let user = LDUser(key: "YOUR_USER_KEY")
var config = LDConfig(mobileKey: "PRIMARY_MOBILE_KEY")
// Note that the SDK throws error strings if you add duplicate keys or put the primary key or name in secondaryMobileKeys.
try! config.setSecondaryMobileKeys(["platform": "PLATFORM_MOBILE_KEY", "core": "CORE_MOBILE_KEY"])
LDClient.start(config: config, user: user)

To access the secondary mobile key instances, use the LDClient.get static method, passing the identifier assigned to your environment key in the secondaryMobileKeys map.

var coreInstance = LDClient.get(environment: "core")
let coreFlagValue = coreInstance?.variation(forKey: "CORE_FLAG_KEY", defaultValue: false)

As all the client instances use the same LDUser object, some SDK functionality affects all instances.

coreInstance.identify(/*User Object*/)
coreInstance.flush()
coreInstance.setOnline(true)
coreInstance.close()

Track calls, listeners, and flag evaluation are all tied to the client instance they are evaluated against.

React Native

Expand React Native code sample

All LDClient instances evaluate against the same LDUser. The environment names and their corresponding mobile keys are specified in a secondaryMobileKeys map passed to your LDConfig object.

You do not need to manage LDClient instances. Instead, access an instance by passing the environment name to a method that supports multiple environments.

let config = {
mobileKey: 'YOUR_MOBILE_KEY',
secondaryMobileKeys: {
'platform': 'PLATFORM_MOBILE_KEY',
'core': 'CORE_MOBILE_KEY'
},
};
let user = {key: 'minimal_user'};
await client.configure(config, user);

To use functionality with a secondary environment, provide the name of 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 learn more, read LDClient.