Skip to content

fix: iOS losing integrations on Setup #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2021

Conversation

nando0208
Copy link
Contributor

@nando0208 nando0208 commented Apr 6, 2021

Issue

Sometimes, mainly on iOS, I'm losing the integration with some libs, like AppsFlyer or Firebase.
We set up as described on device-mode doc but sometimes the received payload on Segment was missing some of the defined integrations.
example of a correct payload passing using: [AppsFlyer, Firebase]:

"integrations": {
    "AppsFlyer": false,
    "Firebase": false
  }

As described on the issue #61 those event informations were being lost and I could find the root cause of the problem.

Wrong Behavior

Debugging I found some intermittent behaviour in this part of the code

This code is using the RNAnalyticsIntegrations to add on integration factories to SEGAnalyticsConfiguration.

This is a static NSMutableSet and it is updated when this integrations are setup.

static NSMutableSet* RNAnalyticsIntegrations = nil;

Setup Method Examples
AppsFlyer
Facebook

However, sometimes the method RNAnalytics.setup is called before RNAnalyticsIntegrations being updated.

I put out some logs while debugging the code and could find that this happens:

2021-03-30 18:23:30.648295-0300 nomad_mobile_app[27521:152492] AF_TEST addIntegration: SEGFirebaseIntegrationFactory
2021-03-30 18:23:30.648317-0300 nomad_mobile_app[27521:152488] AF_TEST addIntegration: BNCBranchIntegrationFactory
2021-03-30 18:23:30.648639-0300 nomad_mobile_app[27521:152503] AF_TEST start USE {(
    <SEGFirebaseIntegrationFactory: 0x600003b9c460>,
    <BNCBranchIntegrationFactory: 0x600003b84830>
)}
2021-03-30 18:23:30.648843-0300 nomad_mobile_app[27521:152461] AF_TEST addIntegration: SEGAppsFlyerIntegrationFactory

Logs added:

RNAnalytics.m, ln 24

+(void)addIntegration:(id)factory {
    NSLog(@"AF_TEST addIntegration: %@", [factory class]);
    [RNAnalyticsIntegrationsLock lock];
    [RNAnalyticsIntegrations addObject:factory];
    [RNAnalyticsIntegrationsLock unlock];
} 

RNAnalytics.m, ln 105

    NSLog(@"AF_TEST start USE %@", RNAnalyticsIntegrations);
    for(id factory in RNAnalyticsIntegrations) {
        [config use:factory];
    }

Hypothesis

My hypothesis was that the following part of the RN code is not waiting correctly for all integrations to be called.

await Promise.all(
using.map(async integration =>
typeof integration === 'function' ? await integration() : null
)
)

I confirmed it putting a sleep on Firebase setup and could check that the code wasn't waiting for all promises to be resolved to continue the execution, so causing the bug.

Fix

Looking on React Native doc I found something about ReactModules and Promises, I made this change on iOS and start to working well. I tested with 5 seconds on Firebase setup and still working.
https://reactnative.dev/docs/0.62/native-modules-ios#promises

Android

I made the change just for iOS because It's our main problem and I'm more comfortable to test.
But it is easy to do the same thing on Android side. Just follow this doc: https://reactnative.dev/docs/native-modules-android#promises

@bsneed bsneed merged commit 640aa73 into segmentio:master Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants