I'm using Firebase in a React Native iOS app, mainly for storing user data and user authentication, which works great when a device actually has a working network connection.
When it comes to Firebase's offline capabilities, it looks like this:
Here are the steps to reproduce this behaviour:
onAuthStateChanged (user)
is being called with the logged in user as parameter user.getToken()
is sent to my server, which generates a custom token (generatedToken
) that can be used for signing into Firebase auth using signInWithCustomToken (generatedToken)
and is therefore saved in local storagegeneratedToken
in local storagegeneratedToken
is used for firebase.auth().signInWithCustomToken (..)
onAuthStateChanged (user)
is not being called with null
as a user
, like it's the case after manually signing out) and can therefore still read & write into Firebase databasegeneratedToken
in local storagegeneratedToken
is used for firebase.auth().signInWithCustomToken (..)
firebase.auth().signInWithCustomToken (..)
fails, because there's not network connectiononAuthStateChanged (user)
is being called with null
as a user
Setting persistenceEnabled
to true
in Objective-C / AppDelegate.m, right after initializing FIRApp
:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
[FIRApp configure];
[FIRDatabase database].persistenceEnabled = YES;
// ...
}
This doesn't lead to the desired result and (at least in my case) doesn't change anything when it comes to Firebase's behaviour.
--- YOUR SUGGESTION HERE ---
Thanks for your inputs!
Persistence BehaviorBy enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache.
To have offline-first functionality when the app is launched and there is no connection, you will need to add redux-persist to your app. This enables it to store a snapshot of the state of your app to the device's memory and rehydrate the state when the app is launched.
Whilst the Firebase JS SDK is great and does generally work in React Native, it is mainly built for web platforms and therefore is not the most comprehensive solution for usage inside a React Native environment e.g. there are quite a few Firebase services you'll be unable to use with the Web SDK. See the comparison table here.
You can, however, run with the native Android/iOS Firebase SDKs and have a bridge between them and your JavaScript code (i.e. a react native module).
Thankfully you don't have to implement this yourself, as there are already modules out there to do this for you:
react-native-firebase for example mirrors the web SDKs API on the JavaScript side but executes on the native side using the native Android & iOS Firebase SDKs. It's compatible with any existing Firebase Web SDK logic that you may have already implemented and is intended as a drop-in replacement for the web SDK.
This library supports your use case with auth persistence and offline capabilities:
import firebase from 'react-native-firebase';
const instance = firebase.initializeApp({
persistence: true
});
// can also use `keepSynced` / `setPersistence` methods:
// instance.database().ref('/someref').keepSynced();
// instance.database().setPersistence(true);
export default instance;
(disclaimer: I am the author of react-native-firebase)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With