Avoiding app startup delays
Every millisecond counts during app launch. Your app is likely to initialize many different components before presenting a live interface to the customer. The LaunchDarkly SDK is one of them. When initializing, the LaunchDarkly SDK attempts these steps:- Open a network connection to the feature server
- Download relevant flag data
- If the SDK has cached flag data from a previous session, then trigger event handlers for flags that have changed since they were cached
LDClient object asynchronously so that it doesn’t block other code from executing.
Initializing asynchronously on specific platforms
The asynchronous initialization process is different for each SDK..NET (client-side) initialization process
We recommend usingLdClient.InitAsync(key, context, timeout) to initialize the SDK, as it runs asynchronously.
The alternative method, LdClient.Init(key, context, timeout) blocks until either it has retrieved flag data or it has exceeded the timeout period.
To learn more, read Get started in the .NET (client-side) SDK reference topic.
Android initialization process
The Android SDK’sLDClient.init() method takes, as its fourth parameter, a timeout integer which specifies the maximum number of seconds the SDK can wait for a response from the server.
Depending on how init() is called, the SDK may block the current thread until either the flags are retrieved or the timeout period is exceeded. If init() is called in the main app thread, the rest of the app startup will be blocked until this call completes, resulting in poor customer experience.
To ensure that LDClient.init() doesn’t block the rest of the app we recommend providing init() a timeout value of 0. The init() method returns an LDClient immediately but continues creating the connection in the background. If cached flag data is available from a previous execution, any invocation of a variation() method will use that cached data until the SDK receives fresh data. Otherwise, it will use the fallback value specified in the invocation. You can also use change listeners to respond when the SDK updates the flag cache.
To learn more, read Get started in the Android SDK reference topic.
iOS initialization process
LDClient.start() always executes asynchronously, to avoid blocking the main thread.
To execute code as soon as flags have been retrieved, you can provide an optional completion handler to start().
To execute code either as soon as flags have been retrieved, or after a set amount of time, whichever comes first, you can also provide a startWaitSeconds timeout value to start(). This sets a maximum waiting period for establishing the connection. We recommend using a timeout of five seconds or fewer. The SDK provides a timedOut boolean to the completion closure, indicating whether the connection timed out.
To learn more, read Get started in the iOS SDK reference topic.
React Native initialization process
In version 10 of the React Native SDK, you create a shared instance ofReactNativeLDClient, and then identify a context. For example, you may call identify() on application mount. End users will receive fallback values until you specify a context by calling identify(). The identify timeout defaults to five seconds.
If you are using older versions of the React Native SDK, the LDClient.configure() method takes, as an optional third parameter, a timeout number, and blocks until either it has retrieved flag data or it has exceeded the timeout period. We recommend setting this timeout to five seconds or fewer.
To learn more, read Get started in the React Native SDK reference topic.
Deciding on the customer experience for flag changes
LaunchDarkly delivers flag updates to your app as soon as possible. This is great for ensuring that flag evaluations are up to date with the current flag settings, but may cause problems depending on how the flag is used. For example, consider a user interface (UI) configured by flags. If the flags change in the middle of a customer’s session, the customer may be confused or alarmed by the interface changing without warning. For each flag used by your app, decide on a policy for how updates to the flag should be handled while the app is running. You may want to ignore updates to certain flags if they happen in the middle of a customer session, whereas other flags, such as circuit breakers, should update immediately. These policies can be implemented as wrapper methods around the LaunchDarkly SDK, rather than configuration changes to the SDK itself.Ensuring delivery of analytics events
The SDK communicates evaluation and diagnostic data back to the server as part of normal usage. The LaunchDarkly UI displays this data in several places, including the Flags list, the flag evaluations graph, and in Experimentation. This data is generated by the SDK and added to an event buffer. The SDK clears the buffer and sends the data back to LaunchDarkly periodically, every few seconds. To ensure that the client delivers any pending analytics to LaunchDarkly, we recommend shutting down the client properly when your application is about to terminate.Try it in your SDK: Shutting down
Try it in your SDK: Flushing events
Targeting the app version number, OS, and device when enabling new features
In newer versions of LaunchDarkly’s mobile SDKs, the SDK can automatically provide data about the mobile environment where the application is running. This data makes it simpler to target your mobile customers based on application name or version, operating system name and version, and device model and manufacturer information. We recommend enabling this feature when you configure the SDK.Configure your SDK: Automatic environment attributes
Configure your SDK: Application metadata configuration