4

I am in process of developing an Android tablet app using sqllite 3.7.4 which would perform following:

  1. Fetches information from the UI

  2. Performs some logic and store related information to the sqlite database

  3. The stored information has to be send immediately OR at schedule interval (ex. at 5:00 on xyz date) over the network

Currently, we have developed a dispacher mechanism (thread ), which constantly polls the database for new information inserted in the database. The thread fetches the information and send to the network module.

But, I feel this is not the correct approach as Polling every time is a overhead. There can be times when there is nothing to execute It is not real time , because we poll after every 5 seconds

So

Is there a way to send a trigger to my network module as soon as information is updated in database?

Or any better way to achieve this task?

Thanks in advance.

Rohit
  • 6,941
  • 17
  • 58
  • 102
  • example of own database listener http://stackoverflow.com/a/13292544/1858599 – mohsin raza Jan 05 '13 at 06:38
  • May be this topic is more relevant to the question asked: http://stackoverflow.com/questions/8783963/android-sqlite-db-notifications – no id Nov 01 '13 at 06:46

4 Answers4

3

This question is about one year ago, but i think this is a common problem. This is how i handled the Database changes:

In my Adapter ( SQL ADAPTER) i have methods for updating / deleting or inserting data into the Database obviously. Like this method:

public long addProduct(String code, String name ... String gid, String gdate) {

  ContentValues initialValues = new ContentValues();
  initialValues.put(KEY_CODE, code);
  initialValues.put(KEY_NAME, name);
  ...
  initialValues.put(KEY_CHECK, this.checkfalse);
  initialValues.put(KEY_GID, gid);

---------
    Intent i = new Intent("data_inserted");
    i.putExtra("date", date);

    sendBroadcast(i);
---------

  return mDb.insert(SQLITE_TABLE, null, initialValues);
 }

After the change happened it will send an broadcast intent. To fetch this first register your Broadcast Receiver in your onCreate Method (uploaderClass or whatever). This will look like this:

registerReceiver(Updated, new IntentFilter("data_inserted"));

And this Method to handle the following actions!

private final BroadcastReceiver Updated= new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        doSomething(); // Data was inserted upload it 
    }
};

EDIT :

To fetch only new items from database, I sign new products. I have a colum "info" which can contain Strings like "upload", "update" or "delete". Then i fetch all Items from the database which contain these special strings and upload them. After that i set them to null or an empty String. Whatever you wish. Hope i explained it not to complicated :)

Markus
  • 449
  • 1
  • 7
  • 22
  • Notifications are _serious_ overkill. At _least_ prefer [ContentObserver](https://developer.android.com/reference/android/database/ContentObserver) and [ContentObservable](https://developer.android.com/reference/android/database/ContentObservable) – Mooing Duck Mar 16 '20 at 00:54
0

You can create your own database listener whenever something is updated to the database it will fetch the information and send to the network. I think will clear some idea for implementing this thing.

Sidharath
  • 130
  • 1
  • 7
-1

Now we can use Room to achieve this.

database.getInvalidationTracker().addObserver(new InvalidationTracker.Observer(tableToObserve) {
        @Override
        public void onInvalidated(@NonNull Set<String> tables) {

        }
    });

database is an instance of RoomDatabase, which is the base class for all Room databases. All classes that are annotated with "@Database" (which is the way to use the Room Libary) must extend this class.

In the creator method of InvalidationTracker.Observer, you can pass in an array of String (or in the form of varargs) to indicate the tables you'd like to observe, if any update happen to those tables, the callback method onInvalidated is invoked.

Some links to refer to: https://developer.android.com/reference/android/arch/persistence/room/InvalidationTracker.html#addObserver(android.arch.persistence.room.InvalidationTracker.Observer)

Yimin
  • 3
  • 4
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post.](https://stackoverflow.com/help/privileges/comment) instead, [provide answers that don't require clarification from the asker.](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead) – Rumit Patel Dec 12 '18 at 06:57
  • @RumitPatel I've updated my answer with code snippet. Thanks for pointing out. – Yimin Dec 12 '18 at 07:21
-2

hope so this will help you

 private Handler h;
    // in create time
        h = new Handler();


// call where you want
h.postDelayed(myRunnable2, 1000); // after 1000 millisecond this function call automatically 

// this function 
private Runnable myRunnable2 = new Runnable() {
        public void run() {
                              // do some thing

                h.postDelayed(myRunnable2, 1000);

        }
    };
mohsin raza
  • 665
  • 1
  • 7
  • 20
  • my query was Is there a way to send a trigger to my network module as soon as information is updated in database? – Rohit Jan 04 '13 at 11:44
  • yap you write in place of do some thing . this function call after every 1000 milliseconds or time you want . – mohsin raza Jan 04 '13 at 11:47
  • How does the answer relates to the question asked? Wasn't it about database triggers? – no id Nov 01 '13 at 06:43