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

    EDIT ON GITHUB

    Multiple environments

    Read time: 2 minutes
    Last edited: Dec 30, 2022

    Overview

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

    Newer versions of LaunchDarkly SDKs replace users with contexts

    A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. To learn more, read Contexts.

    Creating contexts and evaluating flags based on them is supported in the latest major versions of some of our SDKs. For these SDKs, the code samples on this page include the two most recent versions.

    You can upgrade your SDKs at any time, but the ability to target by context, or review context instances that have encountered flags in your application, is only available for customers in the contexts Early Access Program (EAP). If you want access to this feature, join the EAP.

    If you are using the latest version of an SDK and are not part of the EAP, your application can send contexts to LaunchDarkly and they will appear on the Users list.

    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. The SDK can evaluate flags from different environments whether they are all in the same LaunchDarkly project or different LaunchDarkly projects.

    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 LDContext. 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-456def");
    otherKeys.put("core", "core-mobile-key-789ghi");
    LDConfig ldConfig = new LDConfig.Builder()
    .mobileKey("mobile-key-123abc")
    .secondaryMobileKeys(otherKeys)
    .build();
    LDContext context = LDContext.builder("context-key-123abc")
    .set("email", "sandy@example.com")
    .build();
    LDClient.init(this.getApplication(), ldConfig, context);

    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 LDContext object, some calls affect all instances.

    LDClient coreInstance = LDClient.getForMobileKey("core");
    // Calls affect all LDClient Instances
    coreInstance.identify(/*Context 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 LDContext. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig object.

    let context = try LDContextBuilder(key: "context-key-123abc").build().get()
    var config = LDConfig(mobileKey: "mobile-key-123abc")
    // 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-123abc", "core": "core-mobile-key-123abc"])
    LDClient.start(config: config, context: context)

    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-123abc", defaultValue: false)

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

    coreInstance.identify(/*Context 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: 'mobile-key-123abc',
    secondaryMobileKeys: {
    'platform': 'platform-mobile-key-456def',
    'core': 'core-mobile-key-789ghi'
    },
    };
    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.