Skip to content

reactnative, cannot run branch.subscribe() again if first attempt failed #1284

@alainib

Description

@alainib

Describe the bug

Hello

I want to retry initialization of branch but I cannot when first one failed. I guess inner state is stuck somewhere and I cannot restart it fully , I tried with branchUnsubscribe() , await branch.logout();

Mycode:

useEffect(() => {
  let branchUnsubscribe: any;
  let retryCount = 0;

  const subscribeWithRetry = async (error: any) => {

    logError(error, {
      location: 'useDeepLinking.ts',
      description: `subscribeWithRetry ${retryCount} `,
    });
    if (retryCount < MAX_RETRIES) {
      retryCount++;
      if (branchUnsubscribe) {
        console.log('subscribeWithRetry clean branchUnsubscribe?.();');
        branchUnsubscribe?.();
      }
      await branch.logout();

      setTimeout(() => {
        console.log('subscribeWithRetry setTimeout', branchUnsubscribe);
        branchSubscribe();
      }, RETRY_DELAY_MS);
    }
  };

  const branchSubscribe = () => {
    console.log('branchSubscribe ' + retryCount);
    try {
      branchUnsubscribe = branch.subscribe({
        onOpenStart: ({uri, cachedInitialEvent}) => {
          console.log(
            `branchSubscribe onOpenStart, will open ${uri} cachedInitialEvent is ${cachedInitialEvent}`,
          );
        },
        onOpenComplete: ({error, params}) => {
          console.log('branchSubscribe onOpenComplete', {
            retryCount,
            error,
            params,
          });
          if (error) {
            subscribeWithRetry(error);
          } else {
            if (params?.feature) {
              if (
                // If app is not ready or navigation is not ready, save deeplink for later
                appIsReady &&
                isNavigationReady &&
                storeConfig?.loadingStoreConfig !== false &&
                !['SignUpFinished', 'Login'].includes(routeNameRef.current)
              ) {
                forwardChoiceDeepLink(params);
              } else {
                saveDeeplinkDataForLater(params);
              }
            }
          }
          return;
        },
      });
    } catch (error) {
      console.log('error', error);
      subscribeWithRetry(error);
    }
  };

  if (appIsReady && isNavigationReady && isNetworkConnected === true) {
    branchSubscribe();
  }

  return () => branchUnsubscribe?.();
}, [appIsReady, isNavigationReady, isNetworkConnected]);

OUTPUT with no internet :

LOG branchSubscribe 0. // first attempt, normal to fail because no internet

LOG subscribe onOpenStart, will open null cachedInitialEvent is true
LOG onOpenComplete {"error": "Trouble initializing Branch. io.branch.referral.ServerRequestRegisterOpen@13ddc2 failed. -120 Thread task timed out. Timeout: 15500 Task exceeded timeout.", "params": {"+rn_cached_initial_event": true, "error_message": "Trouble reaching server. Please try again in a few minutes"}, "retryCount": 0}
LOG -----------------------------------------------------------
LOG LogError: useDeepLinking.ts: // init call onComplete with error, normal
Trouble initializing Branch. io.branch.referral.ServerRequestRegisterOpen@13ddc2 failed. -120 Thread task timed out. Timeout: 15500 Task exceeded timeout. {"data": undefined, "description": "subscribeWithRetry 0 « }

LOG subscribeWithRetry clean branchUnsubscribe?.(); // try to clean
LOG subscribeWithRetry setTimeout [Function anonymous] // start second attempt in few seconds
LOG branchSubscribe 1 // second attempt, function called but I never get onOpenComplete called oncomplete, it's stuck here !!!

My actual problem is how to call branch.subscribe() multiple times if first times failed ?

Steps to reproduce

  1. cut internet
  2. try initialise
  3. put internet back

Expected behavior

how to run branch.subscribe() if first attempt failed

SDK Version

3.12.1

Make and Model

android simulator or reel device

OS

android 15

Additional Information/Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions