Skip to content
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

Multi-App Realtime Database Use on Android doesn't seem to be working. #1160

Closed
Ayiga opened this issue May 30, 2018 · 5 comments
Closed

Multi-App Realtime Database Use on Android doesn't seem to be working. #1160

Ayiga opened this issue May 30, 2018 · 5 comments
Assignees
Labels

Comments

@Ayiga
Copy link

Ayiga commented May 30, 2018

Issue

Greetings,

It seems as though using a secondary app on the Android platform causes any real-time database request to fail. Any callbacks used for any on, or once query do not seem to resolve.

This does not seem to be an issue on the iOS platform at the moment.

Example snippet:

// file: userApp/index.js
// @flow
import firebase from 'react-native-firebase';

import config from './config';

const UserAppName = 'userApp';

function init(): Promise<any> {
  return firebase
    .initializeApp(config, UserAppName)
    .onReady()
    .catch((err) => {
      l.withError(err).error('initializeApp error');
      throw err;
    });
}

export default init();
// file: other file
// @flow

import userApp from './userApp';

userApp.then((app) => {
  const ref = app
    .database()
    .ref('/some-path-with-data');

  ref.on('child_added', (snap) => { console.log('never called "child_added"'); });
  ref.on('child_changed', (snap) => { console.log('never called "child_changed"'); });
  ref.on('child_removed', (snap) => { console.log('never called "child_removed"'); });
  
  ref.once('value')
    .then((snap) => { console.log('never called "value"'); });
})

Looking through the code, the primary reason for this behavior on the Android side is due to how the database Object is retrieved by the native side. When a database Object is retrieved there is logic that prefers to instantiate the database Object via the database URL (dbURL) itself. As a result, the resulting database Object is associated with the "[DEFAULT]" app instead of the second app.

image

The resulting RNFirebaseDatabaseReference has the correct appName, dbURL, and path internally, but the database Object is associated with the wrong app.

If I were to comment out the conditional, and always fall into the else case instead, I get the database Object with the correctly associated app instead. In this case, all of the queries made end up with results.

image

The easiest solution I can think of is to remove the conditional. However, in doing so dbURL is no longer used within the static function. So it may be necessary to review the reasons for having it there, and if perhaps the underlying function's (FirebaseDatabase.getInstance) behavior has changed or something.

Please let me know if you have any questions, or if you need me to provide any more details,

Thanks,

~ Ayiga

Environment

  1. Application Target Platform:
    Android
  1. Development Operating System:
    macOS High Sierra
    System: MacBook Pro (15-inch, 2017)
    Version: 10.13.4 (17E202)
    Processor: 2.8 GHz Intel Core i7
  1. Build Tools:
    Android Studio 3.1.2
    Build #AI-173.4720617, built on April 13, 2018
    JRE: 1.8.0_152-release-1024-b01 x86_64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Mac OS X 10.13.4
  1. React Native version:
    react-native@^0.55.4
  1. RNFirebase Version:
    react-native-firebase@^4.2.0
    (I believe this issue may have been introduced with version 4.0.0)
  1. Firebase Module:
    database
@Ayiga
Copy link
Author

Ayiga commented Jun 2, 2018

This appears to of occurred in the commit cc8799b which adds support for shards of the database.

So the previously suggested fix is likely not sufficient.

Instead a better solution would be to transform the function to use a different getInstance static function.

Instead, the easiest solution that should support both would be to change the Reference creation code in a manner similar to the following:

  public static FirebaseDatabase getDatabaseForApp(String appName, String dbURL) {
    FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
    FirebaseDatabase firebaseDatabase;
    if(dbURL != null && dbURL.length() > 0) {
      firebaseDatabase = FirebaseDatabase.getInstance(firebaseApp, dbURL);
    } else {
      firebaseDatabase = FirebaseDatabase.getInstance(firebaseApp);
    }

...

Assuming that the appName String is never null, which based on the previous logic seems to be the case.

Please let me know if you have any questions, or if there's something more I can offer.

Thanks,

~ Ayiga

@caseyt
Copy link

caseyt commented Jun 8, 2018

Created a pull request for this issue here: #1193

@Salakar
Copy link
Member

Salakar commented Jun 25, 2018

#1193 merged - should fix Android

iOS still to be fixed. Will take a look or PR's welcome.

@Salakar Salakar added this to the v4.3.0 Release milestone Jun 25, 2018
@Ayiga
Copy link
Author

Ayiga commented Jun 29, 2018

@Salakar I don't believe this affects iOS, only Android.

Based on the code in the initial commit, iOS database reference creation only has one path.

Please let me know if you have any questions,

Thanks,

~ Ayiga

@Salakar
Copy link
Member

Salakar commented Jul 1, 2018

@Ayiga you're right, iOS is setup correctly, will close this issue as Android has been fixed pending release.

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

No branches or pull requests

3 participants