Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Android service context for Room database

I'm working on an App that uses both Firebase Messaging and Room database.

This is how the db is initialized

public static AppDatabase getInstance(final Context appContext) {
    if (INSTANCE == null) {
        INSTANCE = Room.databaseBuilder(appContext, AppDatabase.class, "dbname.db")
                // prepopulate the database after onCreate was called
                .addCallback(rdc)
                .build();
    }
    return INSTANCE;
}

Somewhere on MainActivity the first call to AppDatabase is made and the database is instantiated, all is working fine and dandy!

Now the problem starts when the user kill the app...

For the sake of simplicity think of this App like Whatsapp... the application got killed but the service (MyFirebaseMessagingService) that is listening for messages is still running (as it should).

When a new message arrives and MyFirebaseMessagingService tries to save it to the database with a call to AppDatabase.getInstance() a crash will occur since the initial context reference on witch the database was created no longer exists because the application was killed.

Searching on how to solve this I learned that every service this is actually derived from context so I immediately tried to use the MyFirebaseMessagingService context to initialize the database, however, MyFirebaseMessagingService::onCreate where you really have access to the service context don't get fired until the first message arrives

The only solution I can think of is creating a completely new service, bindService() it to the MainActivity, and use that new service context to create the AppDatabase instance... but it seems completely overkill.

So my question is, how can initialize the Room Database in a way that MyFirebaseMessagingService can still use it even if the App was killed?

like image 668
NickPR Avatar asked Feb 11 '18 15:02

NickPR


People also ask

Is room database deprecated?

This method is deprecated. Called by Room when it is initialized. Convenience method to query the database with arguments. Wrapper for SupportSQLiteDatabase. query .

What is the use of room database in Android?

Room is a persistence library that provides an abstraction layer over the SQLite database to allow a more robust database. With the help of room, we can easily create the database and perform CRUD operations very easily.

What is the difference between SQLite and room database in Android?

Room vs SQLiteRoom provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. In the case of SQLite, There is no compile-time verification of raw SQLite queries. But in Room, there is SQL validation at compile time.


1 Answers

getApplicationContext() is not available in the constructor function. But it's available inside onStartCommand() function. I think that's a good place to build/open the database in a service.

like image 158
Tharanga Avatar answered Oct 19 '22 15:10

Tharanga