How to access room database in service - android

I am having problem with observing LiveData and querying data from Room Database in a foreground service class. This service run in the background and perform various database operations.
Observing LiveData seems possible by using observeForever and
querying can be done by accessing repository class (bypassing ViewModel) but it seems like a hack, not a standard way.

Related

How to safely update data in Realm from background Worker

There is an large app that uses coroutines to update data in local Realm database. Realm is factored and distributed by Dagger 2. All this happens when using the app in foreground. But now I shall update data from background service (FirebaseMessaging).
How to update realm database with data from Worker in WorkManager that is queued from background service. What is ideal approach that will not lead into deadlocks or any other kind of collisions.

Is it possible to create a Queue on Database rows?

Right now I am trying to create a process, where I want to insert data in table and some observers will get notified and able to edit the data upon their interest. Below is a rough idea on how to do it. Can anyone please suggest the model/arch how I can achieve this?
Follow this tutorial on Room, ViewModel, and LiveData.
Room is a SQL database abstraction which can expose its data through LiveData. ViewModel is a class that holds data for the UI and survives configuration changes. LiveData is an observable container for data that is aware of the android lifecycle so you don't have to manage it in the lifecycle callbacks.
Basically, you create a Room database then expose LiveData objects to the ViewModel. The ViewModel in turn exposes LiveData objects to the fragment/activity. The fragment or activity observes the ViewModel's LiveData by attaching an Observer. The Observer defines how the fragment/activity reacts to changes in the data.
If you prefer RXJava you can follow this tutorial instead. It's essentially the same, but instead of exposing data with LiveData you use reactive streams.
Edit: here's a really good article on architecture: https://proandroiddev.com/android-architecture-starring-kotlin-coroutines-jetpack-mvvm-room-paging-retrofit-and-dagger-7749b2bae5f7

Room: observe LiveData to trigger SyncAdapter

I want to request a sync of my sync adapter every time a change happens to some of my data. Having LiveData I wonder if the correct way would be to bind a observer to the data within the application lifecycle.
Does anyone know what the "official" way of doing this is?

Room - when to initialize database and how to access it through app lifecycle

I'm trying to set up Room as a way to simplify access to SQLite database. I've written some code, but I can't run it, as app throws following exception:
Cannot access database on the main thread since it may potentially lock the UI for a long period of time
I've done some research and what I've found is .allowMainThreadQueries() in databaseBuilder which seems to me like terrible solution, because it's like muting error message, not fixing real cause.
So, what are best practices? When (in app lifecycle) should I create my database and where should I store it so I could access it from any Activity I want?
When (in app lifecycle) should I create my database
Lazy-create your RoomDatabase on first access, just as you would when using SQLiteOpenHelper directly.
where should I store it
A singleton will be a typical pattern, just as you would when using SQLiteOpenHelper directly.
Neither of those questions have anything to do with the error message. That is a matter of accessing the database on a background thread, just as you would when using SQLiteOpenHelper directly. For #Query methods, you have the option of having the method return a LiveData or an RxJava type (e.g., Flowable), in which case Room will take care of doing the work on a background thread. For other operations (e.g., #Insert), you are responsible for invoking those methods on a background thread yourself (Thread, AsyncTask, ThreadPoolExecutor, IntentService, JobIntentService, etc.).

How to define callbacks from SQLITE to Android Activity

I want to write a callback from sqlite database to android activity. Currently i am inserting some data from the webservice in to db and then set it to listview.
I am running a service in the background which will continuously check for the new data. If there is new data it will update the database.
My prob is how to define the callback onupdate from sqlite to activity.
Any help will be appreciated.
You should be able to achieve the result with observer pattern.
Your activity will implement an custom interface like RefreshableOnDBUpdateTrigger
in which you will re-load data from DB using standard DB querying mechanisms.
In your DB update class, you can call RefreshableOnDBUpdateTrigger.update() method which is essentially the method of your activity instance which fetches updated data from DB.
Refer observer pattern for implementation details.
This would require user-defined functions, which are not supported by the Android database framework.
You have to call these callbacks explicitly in your database access code.
Another option to the interface callbacks would be to use an Event Bus. The DB code fetches data and then posts an event with the data. Any registered classes would receive the event.

Categories

Resources