23

I am stuck at understanding the concept of making Firebase App Offline. As per the documentation, we need to call:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

But where should we call this? Is it should be used in every Activity of the Application? Because, when I am using this inside my Application class onCreate() method, my app crashes continuously.

So what is the best practice we should follow to make our app offline.

One more doubt is the difference between the above one and DatabaseReference.keppSynced(true);

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125

6 Answers6

21

Create an Application Class

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);

    }
}

And Change your manifest as

<application
    android:name=".MyApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
Jinson Paul
  • 481
  • 6
  • 17
  • do I need to call this when I sue the database? – Rekt Mar 01 '18 at 02:57
  • 5
    Only use this if you need offline properties of firebase database. That is if you are offline then it will show last fetched values when you are online , and if you push something to database while you are offline , this helps to push it when you go online again – Jinson Paul Mar 01 '18 at 05:16
13

setPersistenceEnabled() should be called once on startup, before you retrieve your first reference of the database. I call mine directly after I call FIRApp.configure()

Persistence enabled allows for complete offline retention of information. The significant component of this is that offline requests and updates will be completed, even if you force-close the app and reopen it. If you're looking for offline access that's the boolean to set.

Keep sync is used for if circumstances where you want up to date data cached for whenever you retrieve the information, but don't necessarily need that data to be restored when you next open the app.

A good example of use would be if you had views which accessed the logged in user details. Instead of having user detail listeners on all views which use the details, you can just specify to keep that database reference synced and you can get up to date data quicker.

More details on persistence and syncing can be read here in the Firebase documentation: https://firebase.google.com/docs/database/android/offline-capabilities

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Chris Conway
  • 1,028
  • 7
  • 18
  • 1
    If the app is offline, and there are pending transactions, this section in the documentation says that data is lost on app restart. So after all the offline goodness we get, we still have to manually handle un-committed offline transactions ? https://firebase.google.com/docs/database/android/offline-capabilities#section-handling-transactions-offline – angryITguy Sep 04 '18 at 01:22
  • 1
    @giulio this would be correct. You could potentially address this by using typical data submission, but adding a Node.JS function that observes when that value would be changed, and to react accordingly. eg. You could use Node.JS function to observe when a new message is added to Realtime Database, then add a "DateUploaded" date to the object, which could trigger an observer in the client application to reload that object or set of messages. – Chris Conway Sep 05 '18 at 02:05
3

Taken from here

Not sure, if it is relevant here. But there is another scenario when this crash can happen.

If your app has a service (with different process) and you're creating your own Application class, the service and the foreground app will use the same Application class (not same instance) to initialize. Now when I am using com.google.firebase:firebase-crash dependancy to handle crashes, it creates a background service your.app.packagename:background_crash. For some reason, this was inducing crashes on my app. Specifically, because in my Application class, I was making a call like,

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

I am assuming, the background service when initing with our Application class, somehow Firebase is not initialized. To fix this, I did

if (!FirebaseApp.getApps(this).isEmpty())
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Community
  • 1
  • 1
Omi Mishra
  • 161
  • 1
  • 7
0

If any iOS developer out there facing the same problem, then write the following line in your AppDelegate.swift file in function "application:didFinishLaunchingWithOptions" just before the return statement

FirebaseDatabase.getInstance().setPersistenceEnabled(true); 

and use the following line

DatabaseReference.keppSynced(true);

with your database reference inside your viewController.

0

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

Just paste above line in your very first activity or your launcher activity in onCreate method after set content view. It's working perfectly for me.

  • Hey, thanks for answering! Something you could do to make it better though would be to add some information about **why/how** this solution works. – Jake Sep 03 '19 at 01:01
0

create java class named MyAppOFFLINE and register this class in your manifest file

public class MyAppOFFLINE extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    }
}
Teshie Ethiopia
  • 586
  • 8
  • 27