2

I have a HTTP Server and Android app.

All data -> JSON format. For mapping used Gson.

ORM - ActiveAndroid.

The problem : I need something like an observer/notifier object, which can told me, that a row in database updated just now.

Something like this :

public void interface Observable<T> {
    void onItemUpdated(T item);
}

So I looking a solution. I've read ActiveAndroid docs, but it doesn't get to me any result. Maybe i can mix something with ContentObserver or something like this?

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119

3 Answers3

1

How about to create a logging table where you can append a row for each update. It has the insertion cost but select query is so fast. And also you can store update history of each record if you want or delete the log record if you are worry about performance.

Afshin
  • 977
  • 9
  • 16
  • `And also you can store update history` - how it can be completed? – Sergey Shustikov Nov 21 '15 at 11:59
  • For example your logging table could have "row","action","dateTime". When an insertion in your main table happens, create a row in your logging table and set action = "Insert" and when a row get updated, insert a new row in your logging table with the "UPDATE" value for its action field. :) – Afshin Nov 21 '15 at 12:02
  • `And also you can store update history` = For instance record R1 of table T1 get update at time TIME1. Here you insert a new row into your logging table and name it L1. And again R1 in T1 gets update at TIME2. Here we insert a new row in our logging table and name it L2. Now we have L1 and L2 which show the update history of T1.R1 :) .Hope you got the idea. – Afshin Nov 21 '15 at 12:09
  • Hm.. Actually it not exactly what i need. Yes, with your proposal I really have a time of updates, but I doesn't take a callback in code and even if I notify - separate table - will be useless. – Sergey Shustikov Nov 21 '15 at 12:16
  • Where do you perform insertion into your database? You can send a broadcast message there. I know it's hard to change your code, but it's the best way to achieve what you want. – Afshin Nov 21 '15 at 12:22
  • You can do something good for this to provide less codes. Create a subclass of `SQLiteDatabase` (name it `MySqlite`) and override its `insert` or `execSql` method to perform `super.insert` or `super.execSql` and after that send the broadcast message or call a method of a listener. And in other classes use `MySqlite` instead of `SQLiteDatabase`. – Afshin Nov 21 '15 at 12:33
0

you can create new field in your db which holds the updated date and time and after every operations in db you may query the db about the recent updates done to your db

Anjali
  • 1
  • 1
  • 13
  • 20
0

You can accomplish this by implementing your own ContentObserver for the URI of the table you want to be notified about. ActiveAndroid has its own content provider you can use or you can implement your own, that works with your tables. In one of my projects I used ActiveAndroid and had my own content provider for 3rd party apps. Upon an update/insert/deletion of a row you should be notified by content provider that something was changed with reference to some URI and if you have a registered content observer to this URI you will be notified.

MikeL
  • 5,385
  • 42
  • 41