React-native-firebase: Can't enable persistence

Created on 14 Nov 2017  路  7Comments  路  Source: invertase/react-native-firebase

Issue

I follow the guidelines in order to enable Persistence from the docs in Core/Default-App. However my file MainActivity,java doesn't have any "onCreate()", instead I add the code in MainApplication.java, that it does ontain onCreate() method.

Here my code:

`package com.notamental;

import android.app.Application;

import com.facebook.react.ReactApplication;
import com.wenkesj.voice.VoicePackage;
import com.oblador.vectoricons.VectorIconsPackage;
import io.invertase.firebase.RNFirebasePackage;
import io.invertase.firebase.auth.RNFirebaseAuthPackage;
import io.invertase.firebase.database.RNFirebaseDatabasePackage;
import com.google.firebase.database.FirebaseDatabase;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;

import java.util.Arrays;
import java.util.List;

public class MainApplication extends Application implements ReactApplication {

private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}

@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      new MainReactPackage(),
        new VoicePackage(),
      new VectorIconsPackage(),
      new RNFirebasePackage(),
      new RNFirebaseAuthPackage(),
      new RNFirebaseDatabasePackage()
  );
}

@Override
protected String getJSMainModuleName() {
  return "index";
}

};

@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}

@Override
public void onCreate() {
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
}`

My app/build.gradle file:

`
apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"

/**

  • Set this to true to create two separate APKs instead of one:

    • An APK that only works on ARM devices


    • An APK that only works on x86 devices

  • The advantage is the size of the APK is reduced by about 4MB.
  • Upload all the APKs to the Play Store and people will download
  • the correct one based on the CPU architecture of their device.
    */
    def enableSeparateBuildPerCPUArchitecture = false

/**

  • Run Proguard to shrink the Java bytecode in release builds.
    */
    def enableProguardInReleaseBuilds = false

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.notamental"
    minSdkVersion 16
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
    ndk {
        abiFilters "armeabi-v7a", "x86"
    }
}
splits {
    abi {
        reset()
        enable enableSeparateBuildPerCPUArchitecture
        universalApk false  // If true, also generate a universal APK
        include "armeabi-v7a", "x86"
    }
}
signingConfigs {
    release {
        if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
            storeFile file(MYAPP_RELEASE_STORE_FILE)
            storePassword MYAPP_RELEASE_STORE_PASSWORD
            keyAlias MYAPP_RELEASE_KEY_ALIAS
            keyPassword MYAPP_RELEASE_KEY_PASSWORD
        }
    }
}
buildTypes {
    release {
        minifyEnabled enableProguardInReleaseBuilds
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        signingConfig signingConfigs.release
    }
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
    variant.outputs.each { output ->
        // For each separate APK per architecture, set a unique version code as described here:
        // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
        def versionCodes = ["armeabi-v7a":1, "x86":2]
        def abi = output.getFilter(OutputFile.ABI)
        if (abi != null) {  // null for the universal-debug, universal-release variants
            output.versionCodeOverride =
                    versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
        }
    }
}

}

dependencies {
compile project(':react-native-voice')
compile project(':react-native-vector-icons')
compile(project(':react-native-firebase')) {
transitive = false
}
compile "com.google.android.gms:play-services-base:11.4.2"
compile "com.google.firebase:firebase-core:11.4.2"
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+" // From node_modules
compile "com.google.firebase:firebase-auth:11.4.2"
compile "com.google.firebase:firebase-database:11.4.2"
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}

apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
apply plugin: 'com.google.gms.google-services'
`
But when I console.log any Reference, it appears persistence: false as an option:

image

Environment

  1. Application Target Platform: Android

  1. Development Operating System: Windows 7

  1. Build Tools: React-native - AVD

  1. React Native version: "react-native": "0.49.3",

  1. RNFirebase Version: "react-native-firebase": "^3.0.5",

  1. Firebase Module: Database
Database

Most helpful comment

For future readers, this is documented here: https://rnfirebase.io/docs/v3.2.x/core/default-app#Enable-Database-Persistence

All 7 comments

@aamping A couple of things:

1) We thought we'd updated the docs to use MainApplication instead of MainActivity but missed a few places, which I've now corrected.
2) Don't worry, persistence has been enabled correctly - unfortunately as this is set in the native code, we currently have no way of reading it back out to the JS code, so you're just seeing an incorrect value on the flag. We'll have a think if we can address this in someway, but be assured, persistence is definitely enabled if you're marking it as so in MainApplication

I see, thanks @chrisbianca

I believe this should be something related with Firebase server, cause the ref come from there..

Marking this a dupe

Some thing very similar is happening for me. But in my case, as I mentioned, I can't seem to fetch the data without network connectivity. @aamping have you tried persisting data on iOS? @chrisbianca could you please take a look at my setup for iOS and validate?
Thanks.

I didn't have to do this in the past but I needed to add this to get offline to work in XCode:
[FIRDatabase database].persistenceEnabled = YES;

I already had in javascript:
const configurationOptions = {
debug: true,
persistence: true,
};

const firebase = RNFirebase.initializeApp(configurationOptions);

Using:
Firebase iOS SDK 4.8.0
RNFB: 3.2.0
RN: .50.3

Yup was having the same issue as @jacksontbryan but once I added "[FIRDatabase database].persistenceEnabled = YES;" everything seemed to work offline again.

For future readers, this is documented here: https://rnfirebase.io/docs/v3.2.x/core/default-app#Enable-Database-Persistence

Was this page helpful?
0 / 5 - 0 ratings