hbmartin / cordova-plugin-firebase-realtime-database

Cordova plugin for Google Firebase
13 stars 8 forks source link

Calling setDatabasePersistent crashes the app #4

Open homerlex opened 8 years ago

homerlex commented 8 years ago

Using cordova 6.3.1 and testing on Android.

I was able to create an app with your plugin that updates values in firebase. However, once I added the window.FirebaseDatabasePlugin.setDatabasePersistent(true); line to the code the app crashes hard and I don't know what the exception is. FYI - window.FirebaseDatabasePlugin.setDatabasePersistent(false); does the same thing.

I tried catching and and setting an error but nothing gets reported back to my app. With the following code the app doesn't crash but I don't see the error reported anywhere either:

            try{
                    FirebaseDatabase.getInstance().setPersistenceEnabled(persistent);
                } catch (Exception e) {                    
                    callbackContext.error(e.getMessage());
                }

Are there any special permission or other plugins needed to use this option?

highfieldjames commented 7 years ago

I have the same issue. Did you ever find a solution @homerlex ?

homerlex commented 7 years ago

@highfieldjames - No, I haven't messed with it since posting this issue.

homerlex commented 7 years ago

I got it running in Android Studio so I can see what is going on. Here is the exception:

com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
                         at com.google.firebase.database.FirebaseDatabase.zzrh(Unknown Source)
                         at com.google.firebase.database.FirebaseDatabase.setPersistenceEnabled(Unknown Source)

The docs (https://firebase.google.com/support/guides/firebase-android) state: Like in the 2.x SDK, enabling disk persistence must be done before any other calls to the database are made.

So, in FirebaseDatabasePlugin.java I modified pluginInitialize adding the setPersistenceEnabled call there to see if it would work - and it does:

    protected void pluginInitialize() {
        final Context context = this.cordova.getActivity().getApplicationContext();
        this.cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                Log.d(TAG, "Starting Firebase plugin");
                FirebaseDatabase.getInstance().setPersistenceEnabled(true);
                mDatabase = FirebaseDatabase.getInstance().getReference();
                mAuth = FirebaseAuth.getInstance();
            }
        });
    }

If setPersistenceEnabled is called after FirebaseDatabase.getInstance().getReference(); the error occurs. Obviously the FirebaseDatabasePlugin class needs to be rejiggered a bit if you want to be able to make the persistence setting decision from the JS code.

I haven't played with it any further than that yet but now we know what the problem is :-).

PedroFerraz2012 commented 4 years ago

guys, I think it's a long time nobody is paying attention to this... but I am playing with my Cordova app, getting Firebase to work straight connected or through a server.. The thing is, this try and catch with setPersistenceEnabled(true) worked for me, then my app isnt crashing anymore. tks

hbmartin commented 4 years ago

@PedroFerraz2012 Correct. Apologies I no longer maintain this project. I happy to work with any new maintainer to hand it off.