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