segmentio / analytics-kotlin

The hassle-free way to add Segment analytics to your Kotlin app (Android/JVM).

Repository from Github https://github.comsegmentio/analytics-kotlinRepository from Github https://github.comsegmentio/analytics-kotlin

UpdateType.Initial called 2 times for a plugin destination

dariusz-survicate opened this issue · comments

Describe the bug
When analytics is initialized in Application's onCreate method with the below code snippet, the UpdateType.Initial of a destination is called 2 times instead the expected 1 call. The problem does not exist when initializing Analytics inside onCreate of Activity.

analytics = Analytics(segmentKey, applicationContext) {
     flushAt = 3
     flushInterval = 10
}
analytics.add(CustomDestination(applicationContext))

To Reproduce
Steps to reproduce the behavior:

  1. Initialize Analytics inside onCreate method of the Application class
  2. Add custom destination right after initialization
  3. The DestinationPlugin.update is called 2 times in a row with UpdateType.Initial

Expected behavior
The DestinationPlugin.update should be called 1 time with UpdateType.Initial.

Platform (please complete the following information):

  • Library Version in use: 1.13.2
  • Platform being tested: [e.g. Android] Android API 33 Emulator
  • Integrations in use: Custom plugin destination

@dariusz-survicate thanks for reporting this issue. we've found the root cause. will release a new version soon

@dariusz-survicate the issue is fixed in the latest release 1.13.3. please try it out

@wenxi-zeng In the 1.13.3 the problem is that Initial update is usually not called at all: the first is Refresh and nothing more, so the following snippet does not initialize our SDK:

 override fun update(settings: Settings, type: Plugin.UpdateType) {
        super.update(settings, type)

        if (type == Plugin.UpdateType.Initial) {
            val workspaceKey = settings.destinationSettings<SurvicateSettings>(key)?.workspaceKey
            if (workspaceKey != null) {
                Survicate.setWorkspaceKey(workspaceKey)
            }
            Survicate.init(context.applicationContext)
        }
    }

Looks like there is a race condition, because sometimes (randomly?) the Initial update is called correctly as the first.

hey @dariusz-survicate, yes, that's a race condition, since analytics is initialized asynchronously, the settings may not be available at the time the plugin is added, thus Initial is not always passed.

we're working on a fix, but it's a bit more of stretch. in the meanwhile, please use a local variable to indicate whether the plugin is initialized instead of depending on the update type.

@dariusz-survicate the race condition should be fixed completely in 1.13.4, please have another try and let us know if your problem persists.

It's now fixed. Thank you for quick solution!