0

How can I fix the following Error inside my activity?

Caused by: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.

I set setPersistenceEnabled() like that in my activity:

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
    when (item?.itemId) {
        R.id.ViewThat_add -> {
            val intent = Intent(this, Add::class.java)
            startActivity(intent)

        }
        R.id.ViewThat_delete -> {
            delete()
        }
        R.id.ViewThat_Download -> {
            enablePersistence()
            download()


        }


    }
    return super.onOptionsItemSelected(item)

}

private fun enablePersistence() {
    FirebaseDatabase.getInstance().setPersistenceEnabled(true)
}

Inside the documentation it was made very similar to this, I don't really know how to fix this error.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
L. Busekrus
  • 412
  • 1
  • 4
  • 11

1 Answers1

1

As the error message says you need to enable persistence before making any other calls to the database. A common way to do this is to put the call into the onCreate of your main activity, or in a subclass of Application, or a ContentProvider.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • class ViewPlaylist : AppCompatActivity() { private fun enablePersistence() { FirebaseDatabase.getInstance().setPersistenceEnabled(true) } override fun onCreate(savedInstanceState: Bundle?) { enablePersistence() super.onCreate(savedInstanceState) setContentView(R.layout.activity_view_playlist) setPlaylistInfo() } I have made like that but the activity still crashes. – L. Busekrus Dec 16 '18 at 11:25
  • Interesting. It means that there must be some other call to the database before you enable persistence. Have a look at some of the other questions about the same error message, such as [this](https://stackoverflow.com/questions/37753991/com-google-firebase-database-databaseexception-calls-to-setpersistenceenabled), [this](https://stackoverflow.com/questions/37448186/setpersistenceenabledtrue-crashes-app) or [this](https://stackoverflow.com/questions/41250507/android-firebase-setpersistenceenabledtrue-crashing-the-app). – Frank van Puffelen Dec 16 '18 at 14:53
  • I fixed the problem I think but can I some how check that the data has been stored? – L. Busekrus Dec 16 '18 at 15:38
  • Run the app, disable the network, run the app again? – Frank van Puffelen Dec 16 '18 at 15:40
  • Ah yeah I forgot the whole purpose of that :D. Thank you again! – L. Busekrus Dec 16 '18 at 15:44
  • This question comes really late but I have done some testing and I noticed that when I switch between fragments the enablePersistence() don't work even when I put as first in onCreate, any idea how to fix that? Because the problem remains even when I write the enablePersistence() call at first in the fun that call the fragment in the Main Activity. Also the app crashes when I return from a non Firebase fragment to a Firebase fragment were enablePersistence() is included, I don't really need it its just something I noticed... – L. Busekrus Dec 17 '18 at 19:19
  • I have no idea why it would't work when using fragments, but would recommend using an `Application` subclass or `ContentProviders`. But if you have a (new) problem with persistence in those situations, I'd recommend checking out [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) and create a new question with that. – Frank van Puffelen Dec 17 '18 at 19:28
  • I have found the answer to that , you have to write the enablePersistence() call inside the Activity where the fragments transactions are handled. I will create a new post and share this solution just in case somebody has that same problem – L. Busekrus Dec 18 '18 at 16:49