Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

LDClient not initialised [Error: LDClient.getForMobileKey() was called before init()!] #186

Closed
vkundapur opened this issue Feb 27, 2023 · 12 comments

Comments

@vkundapur
Copy link

vkundapur commented Feb 27, 2023

Describe the bug
On launch of the app, I am getting the following exception while trying to initialise the SDK in Android.
Launch Darkly LDClient not initialised [Error: LDClient.getForMobileKey() was called before init()!]

To reproduce
Here is the code that are running to initialise the SDK:

// LaunchDarkly/index.ts
// Launch Darkly Singleton client
export default class LaunchDarkly {
  static client: LDClient | null = null

  static async initialiseLDClient(user: LDUser) {
    if (!LaunchDarkly.client) {
      console.log('Launch Darkly', 'creating new LDClient')
      LaunchDarkly.client = new LDClient()
    }
    const ldConfig: LDConfig = { mobileKey: Config.LAUNCH_DARKLY_SDK_KEY }
    try {
      const isInitialised = await LaunchDarkly.client.isInitialized()
      if (isInitialised) {
        console.log('Launch Darkly', 'LDClient already initialised')
        return
      }
    } catch (err) {
      console.log('Launch Darkly', 'LDClient not initialised', err)
    } finally {
      await LaunchDarkly.client.configure(ldConfig, user)
    }
  }
}

Initialisation of the client is done in the code below:

  useEffect(() => {
    LaunchDarkly.initialiseLDClient({ key: ANONYMOUS_USER_ID, anonymous: true })
      .then(() => {
        console.log('Launch Darkly', 'Launch Darkly initialised successfully')
      })
      .catch(err => {
        console.log('Launch Darkly', 'Launch Darkly initialisation failed', err)
        trackError(
          'Launch Darkly initialisation failed',
          {},
          ErrorSource.CUSTOM,
          err,
        )
      })
  }, [trackError])

The SDK returns an exception during initialisation but then automatically recovers in the next run as you can see from the logs.

Expected behaviour
There should be no exception thrown.

Logs
'Launch Darkly', 'LDClient not initialised', [Error: SDK has not been configured]
'Launch Darkly', 'Launch Darkly initialised successfully'

SDK version
"launchdarkly-react-native-client-sdk": "^6.3.0",

OS/platform
Android OS v13.0
"react-native": "0.70.5",

@louis-launchdarkly
Copy link
Contributor

Hello @vkundapur, thank you for reaching out about this issue. I will need to discuss this with the engineer on the team who is more familiar with React lifecycle - this seems like the client was accessed too soon (before the initialization).

In the meantime, given you are using React Native 0.70, we also recommend you migrated to use the 7.x version of the LaunchDarkly React Native SDK, as that has more stability fixes in addition to the support of Contexts.

Filed Internally as 190729.

@louis-launchdarkly
Copy link
Contributor

Hello @vkundapur, can you try the code with the latest version of the SDK 7.1.2 and see is this still an issue that is happening? We recently released more fixes, and I want to know was that resolved the issue, or if this is still something we need to look into more.

@vkundapur
Copy link
Author

Thanks @louis-launchdarkly. I'll check and get back to you on this.

@vkundapur
Copy link
Author

@louis-launchdarkly I am still getting the same error after updating to SDK 7.1.2.

@vkundapur
Copy link
Author

@louis-launchdarkly Any update on this? Let me know if you need any additional information.

@mikevercoelen
Copy link

We're also getting this problem, and it's very annoying because it crashes our expo-dev-client

@Soft-Gurus
Copy link

Soft-Gurus commented Apr 20, 2023

@louis-launchdarkly @vkundapur
Same issue in 7.1.x with RN 0.71.6 in android only

For me downgrading to 6.2.2 worked once. i.e. when app is first launched it shows all flags. when quit and restart, it does not show. strange

@louis-launchdarkly
Copy link
Contributor

Hello @vkundapur,

Looking at your code, is there a reason you called LaunchDarkly.client.isInitialized() before calling aunchDarkly.client.configure(ldConfig, user)? Looking at https://github.com/launchdarkly/react-native-client-sdk/blob/main/index.d.ts#L610-L620 - isInitialized() is checking does the SDK has a mobile key and is ready to serve flags, but your configure call is in the finally portion of the code, so the client is not initialized the first time you check it.

@vkundapur
Copy link
Author

@louis-launchdarkly The intent is to see if SDK is already initialise and not call the LaunchDarkly.client.configure method if it is already initialised.

@louis-launchdarkly
Copy link
Contributor

Look at the test app we provided with the SDK https://github.com/launchdarkly/react-native-client-sdk/blob/main/ManualTestApp/App.tsx#L67

I think when you see the client does not exist, you want to do a configure call there:

    const ldConfig: LDConfig = { mobileKey: Config.LAUNCH_DARKLY_SDK_KEY }
    if (!LaunchDarkly.client) {
      // If we get to this line, the client does not exist and won't be initialized yet so it is safe to call configure once
      console.log('Launch Darkly', 'creating new LDClient')
      LaunchDarkly.client = new LDClient()
      await LaunchDarkly.client.configure(ldConfig, user)
    }

@louis-launchdarkly
Copy link
Contributor

Hello @mikevercoelen and @Soft-Gurus, as I mentioned in the message above, we have a ManualTestApp that demos the init process https://github.com/launchdarkly/react-native-client-sdk/blob/main/ManualTestApp/App.tsx#L30 and I was not able to reproduce the issue using that code, so I would recommend trying to model your init like the test app accordingly. Or if you can provide your code, either on this issue (or if you want privacy, via a Support ticket), we can try to see what's wrong in your case.

@louis-launchdarkly
Copy link
Contributor

Closing as there is no further feedback/comment.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants