Skip to content

11.9.0-experimental.1: payload filters and deferred environments (experimental)

Pre-release
Pre-release

Choose a tag to compare

@abelonogov-ld abelonogov-ld released this 26 Nov 01:00
· 7 commits to v11 since this release
Immutable release. Only release title and notes can be modified.

Warning

Experimental prerelease, not for production. It is built from the andrey/apple-test-app-two-environments branch (commit a4f6dfbe), which includes pull requests that are still in review. The APIs below may change before they ship in a regular release. This build is published here only: it is not on CocoaPods, and the SDK still reports its version as 11.8.1.

It's based on 11.8.1 and adds two features for apps that use several LaunchDarkly environments: payload filters and deferred secondary environments.

Installing

With Swift Package Manager, pin the exact tag:

.package(url: "https://github.com/launchdarkly/ios-client-sdk.git", exact: "11.9.0-experimental.1")

Payload filters (#548)

A payload filter limits the flags LaunchDarkly sends to an environment, so the SDK downloads and stores only the flags the app needs. Filters are defined per project in LaunchDarkly; the SDK sends the filter's key with its streaming and polling requests.

Each environment has its own filter:

  • LDConfig.payloadFilter applies to the primary environment only.
  • A secondary environment gets its filter from addSecondaryEnvironment(_:mobileKey:payloadFilter:). Use a filter that is defined in that mobile key's project.
  • An environment without a filter receives every flag, as before. Apps that don't use filters need no changes.
var config = LDConfig(mobileKey: "mob-primary-key", autoEnvAttributes: .enabled)
config.payloadFilter = "mobile-home"                        // primary environment

try config.addSecondaryEnvironment("casino",
                                   mobileKey: "mob-casino-key",
                                   payloadFilter: "casino-lobby")   // filtered secondary
try config.addSecondaryEnvironment("sports",
                                   mobileKey: "mob-sports-key")     // unfiltered secondary

LDClient.start(config: config, context: context, startWaitSeconds: 5)

Things to know:

  • An invalid filter key is logged as an error, and that environment requests all of its flags. A valid key contains only letters, digits, ., _ and -, and starts with a letter or digit.
  • Payload filters are an FDv1 feature, and this SDK uses FDv1. A mobile key that has a custom payload (views linked to the key) is meant for FDv2's view filtering. LaunchDarkly rejects this SDK's requests made with such a key, with a 422 status.
  • Read a filter back with config.payloadFilter(forEnvironment:).
  • setSecondaryMobileKeys(_:) replaces every secondary environment. An environment it lists again under the same name and mobile key keeps its filter. A new name, or a name with a different mobile key, has no filter.
  • Objective-C has payloadFilter, addSecondaryEnvironment:mobileKey:payloadFilter:error: and payloadFilterForEnvironment: on LDConfig.

Deferred secondary environments (#576)

An app with several environments can now hold back the flag requests of the less important ones. The environments that gate the first screen then load first, without competing for bandwidth.

Add an environment with deferred: true. LDClient.start creates its client like any other, but the client makes no flag request until you start that environment.

try config.addSecondaryEnvironment("casino", mobileKey: "mob-casino-key",
                                   payloadFilter: "casino-lobby", deferred: true)
try config.addSecondaryEnvironment("poker", mobileKey: "mob-poker-key", deferred: true)

LDClient.start(config: config, context: context, startWaitSeconds: 5) { timedOut in
    // The primary and non-deferred secondary environments are ready (or timed out).
    // Now start the deferred ones, one after another, in the order they were added.
    LDClient.startDeferredEnvironments(startWaitSeconds: 5) { timedOut in
        // Every deferred environment has its flags, or timedOut is true.
    }
}

Starting deferred environments:

  • LDClient.startDeferredEnvironments(inOrder:startWaitSeconds:completion:) starts them one at a time. It starts each one once the one before it has its flags or has waited startWaitSeconds. Without inOrder, it uses the order you added them in; pass a list of names (most important first) to choose the order.

  • LDClient.start(environment:startWaitSeconds:completion:) starts a single environment:

    LDClient.start(environment: "casino", startWaitSeconds: 3) { timedOut in
        // casino has flags LaunchDarkly sent for the current context, or timedOut is true.
    }

You don't need to track which environments are deferred to call start(environment:). It works for any environment and always means "wait until this environment has flags from LaunchDarkly for the current context". For an environment that isn't deferred, or one already started, it starts nothing new. It completes at once if those flags are already there, or waits for them otherwise, for example right after identify. Flags cached on the device don't count.

Before a deferred environment is started:

  • LDClient.get(environment:), flag evaluations, observers and identify all work. Evaluations return flags cached on the device for the current context, or the default values when there are none.
  • It makes no flag requests. It records events and sends them while the client is online.
  • The completions of LDClient.start and identify don't wait for it.
  • isDeferred is true and isInitialized is false.

Offline and backgrounded apps: if the client is offline, or in the background without background updates enabled, there's nothing to start yet:

  • start(environment:) calls its completion with timedOut == true right away. That environment's flag connection starts the next time the client goes online.
  • startDeferredEnvironments stops and leaves the remaining environments deferred. Call it again once the client is online to start them in order.
  • It also stops if the client is closed or LDClient.start is called again before it finishes.

Other details:

  • Checking deferral: config.isDeferred(forEnvironment:) reports whether an environment was added as deferred. LDClient.isDeferred reports whether a client is still waiting to be started.
  • setSecondaryMobileKeys(_:): an environment it lists again under the same name and mobile key stays deferred.
  • Objective-C: addSecondaryEnvironment:mobileKey:payloadFilter:deferred:error: and isDeferredForEnvironment: on LDConfig; startWithEnvironment:startWaitSeconds:completion:, startDeferredEnvironmentsInOrder:startWaitSeconds:completion: and isDeferred on LDClient.

Also on this branch

These are also unreleased and still in review. They are included because the branch builds on them:

  • Event durability, opt-in and experimental (#530, #536, #538):
    • New LDConfig settings: eventPersistence (off by default), eventCapacityBytes, eventMaxAge (24 hours by default, #549) and eventDeliveryDelay.
    • New flush methods on LDClient: flush(timeout:completion:), which reports a FlushResult, and flushAndWait(timeout:). The existing flush() is unchanged.
  • A test app for iOS, Mac Catalyst, macOS, tvOS and watchOS (#545, #550). It can run primary, secondary and deferred environments, each with an optional payload filter, and shows each environment's loading state.