• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL

EDIT ON GITHUB

Multiple environments

Read time: 1 minute
Last edited: Jul 28, 2021

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

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.

1Map<String, String> otherKeys = new HashMap<String, String>();
2otherKeys.put("platform", "PLATFORM_MOBILE_KEY");
3otherKeys.put("core", "CORE_MOBILE_KEY");
4
5LDConfig ldConfig = new LDConfig.Builder()
6 .mobileKey("MOBILE_KEY")
7 .secondaryMobileKeys(otherKeys)
8 .build();
9
10LDUser user = new LDUser.Builder("user key")
11 .email("fake@example.com")
12 .build();
13
14LDClient.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.

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

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

1LDClient coreInstance = LDClient.getForMobileKey("core");
2
3// Calls affect all LDClient Instances
4coreInstance.identify(/*User Object*/);
5coreInstance.flush();
6coreInstance.setOffline();
7coreInstance.setOnline();
8coreInstance.close();

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

iOS

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.

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

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

1var coreInstance = LDClient.get(environment: "core")
2let 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.

1coreInstance.identify(/*User Object*/)
2coreInstance.flush()
3coreInstance.setOnline(true)
4coreInstance.close()

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

React Native

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.

1let config = {
2 mobileKey: 'YOUR_MOBILE_KEY',
3 secondaryMobileKeys: {
4 'platform': 'PLATFORM_MOBILE_KEY',
5 'core': 'CORE_MOBILE_KEY'
6 },
7};
8let user = {key: 'minimal_user'};
9await 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.