0

This problem is for Xamarin Android C# , but if someone can help in java I'm sure I can convert the code over ..

I'm trying to get some sort of automatic notification on a db that data has been inserted / deleted / etc.

There is outside apps that have access to the db in question, that insert / etc..

I've tried a file observer but it misses most of the inserts. I've tried using content observer but it never fires a onchange I've tried using the content observer inside a cursor but no onchange happens either.

(if I understand correctly they will only fire if I register a change occurred which is what I don't want)

Now I've discovered that loaders might be a solution.. They seem to have their own observer that fires when the data changes.

If this is also not an answer then perhaps a database trigger of some kind to notify my app the data was modified ?

I really need guidance here.. no idea how to properly implement a loader.. or if the content observer can be sufficient somehow with some sort of auto trigger as such..

Michiluki
  • 505
  • 3
  • 17
Migz
  • 163
  • 1
  • 13
  • SQLite, Realm, Cupboard? Which kind of DB? Possible duplicate of http://stackoverflow.com/questions/8783963/android-sqlite-db-notifications – Xavjer Nov 24 '15 at 09:29
  • the android sqlite db .. yes almost same question but I have outside forces working on my db too.. need to be auto notified in some sort of fashion without constantly polling the db for ".count" changes .. – Migz Nov 24 '15 at 09:34
  • see [this](http://developer.android.com/guide/components/loaders.html#example) – pskink Nov 24 '15 at 10:13
  • dam .. I see that the loader also uses a NotifyChange() .. – Migz Nov 24 '15 at 10:51
  • Think I will have to revisit the file observer and see If I cannot make it more robust somehow .. it's the only observer I have come across that auto triggers .. If that fails then I'm out of luck and will need to poll ... – Migz Nov 24 '15 at 10:53
  • "file observer"? what file observer are you talking about? – pskink Nov 24 '15 at 11:01
  • [link](http://stackoverflow.com/questions/33703113/android-fileobserver-example-in-xamarin-c) .. this has been tested and "Kinda works" .. misses a bit thou.. – Migz Nov 24 '15 at 11:06

1 Answers1

0

OK, So!

The reason that file Observer was missing db inserts is because a SQLite DB is actually a 2 - 3 file object .. if you watch "Example.DB" then you can miss insert that can happen on Example.DB-shm or Example.DB-wal..

The fix to this, and its not a great fix, is to instead watch the folder ..

doing this will catch all inserts/deletes ..

The problem with this is that it will cause multiple OnChange() 's to execute.. So when coding a file observer like this, you have to either call the stopwatch() while you process the call and switch it on afterwards

OR

Have a "Global Variable" (C# guys are gonna swear at me for calling it that) or static var that lets the app know that you are already busy on the event so don't execute the code till the previous call has been completed ..

Migz
  • 163
  • 1
  • 13