Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observing @Relation changes in a DataBase

If I something like the following:

@Relation(parentColumn = "id", entityColumn = "fk_id", entity = User.class)
private List<User> users;

This list is a model which gets updated via LiveData.

But I want to be able to update this when I detect changes to the User table. Right now, it only picks up what exists in the DB, but whenever an update happens to the user table, this list does not get updated. Any suggestions?

like image 399
Nitrodbz Avatar asked Mar 12 '26 11:03

Nitrodbz


1 Answers

You have not been very specific in what you are doing or want (which database you use, who is updating the database...).

But I want to be able to update this when I detect changes to the User table. Right now, this only picks up what exists in the DB but whenever an update happens to the user table, this list does not get updated. Any suggestions?

Option 1

If you are doing the update/insert/delete in the same app as the List users, then you can easily update your List in your code.

Option 2

If another app/service is updating the database, I suggest you use a trigger to update your List.

See http://www.coderconsole.com/2015/02/android-sqlite-trigger-demo.html for an example.

Detecting the change in Java is tricky, see Calling a Java method from a sqlite trigger (android) .

Here is an example where you use a ContentObserver to monitor your Sqlite database (the flat file itself) https://gist.github.com/JBirdVegas/3874450

Option 3

If another app/service is updating the database, you can use a BroadcastReceiver to pick up the changes.

like image 188
Jon Goodwin Avatar answered Mar 14 '26 23:03

Jon Goodwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!