0

So guys I have my app working 100% with Firebase and react-native but the problem is if I try to disconnect the internet the app don't get cached information.

On official Firebase says that firebase works with local storage cache and provides off-line information, but how?

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56

1 Answers1

1

If you are using JavaScript SDK, it will not work on mobile apps.

To achieve this feature, you have to add react-native-firebase, which is an abstraction of JavaScript SDK to both iOS and Android SDK.

https://rnfirebase.io/

After adding it to your project, you can setup the following config:

Android

Add FirebaseDatabase.getInstance().setPersistenceEnabled(true); inside your MainApplication.java files onCreate() method.

You also need to explicitly add the FirebaseDatabase import with the rest of the imports near the top of your MainApplication.java file:

import com.google.firebase.database.FirebaseDatabase;

iOS

Add [FIRDatabase database].persistenceEnabled = YES; after the [FIRApp configure]; line inside your AppDelegate.m files didFinishLaunchingWithOptions method.

Source: https://rnfirebase.io/docs/v3.2.x/core/default-app#Enable-Database-Persistence

Also, if you want to trigger any event when the user lost connection, you can use onDisconnect listener: https://rnfirebase.io/docs/v4.2.x/database/reference/OnDisconnect

Hope it helps

soutot
  • 3,531
  • 1
  • 18
  • 22