Expiration timer - android

I have a record in my Android DB which is saved along with its creation time. I want to have a timer where after 2 hours this item will be deleted. I researched it and I came to two results:
one was to make an AsyncTask, but this solution seems to me like it would take too many resources without any need.
the second was to implement a Service. I know nothing of Services in Android and how they work. I made some reading and I don't see how I can have a service counting time and checking if it has exceeded its lifespan.
Halp plz.

I suggest that you could give a look into the AlarmManager Class
http://developer.android.com/reference/android/app/AlarmManager.html

Why not simply ignore Rows that are older than 2hours? Simply create a VIEW on your Table. Paired with an INSERT/UPDATE Trigger on the DB that delete old Records.
pseudo:
for the view select
select * from table where createdate > now()-2hours
create trigger on table.cleanuptrigger after insert/update
on table for each row when createdate < now()-2hours;

I have found that `"CountDownTimer" works well as long as your application is alive.

Related

How to know changing data of postgresql in last 5 seconds

I am developing an Odoo 8 (OpenERP) application. That application use postgresql database. In backend of Odoo 8, there is add sale order button. So, I want to know, how to know the changing data in the last 5 seconds? My need is I want to insert data from mobile apps. What tables that that changing? Any query for do that? Or another suggest.
the database has 314 tables. Is there application of part3 like MONYog may be?
Any help is very appreciate.
For this kind of situation, there is one best way to manage as
Use always two columns in each table as "CreatedOn" and "LastUpdatedOn", and insert proepr values in it as on create time add current time in both then on every update just change last updated on by current time so you will easily get data as per your requirement.
You could also add a function like this:
create or replace function notify_table_change() RETURNS TRIGGER
LANGUAGE PLPGSQL AS
$$
NOTIFY all_writes TG_RELNAME;
IF TG_OP = 'DELETE' THEN RETURN OLD; ELSE RETURN NEW; END OF;
$$;
Then you could add the trigger for all inserts, updates, and deletes (but not truncate since I didn't handle that).
Then te client can:
LISTEN all_writes;
And will get live notifications of which tables are updated in real time.
Monitoring database feels a bit strange approach to the problem. Writing custom modules for odoo (and OpenERP) is very simple and staightforward. I'd create module which triggers whatever you want to do.
Here is a brief example of simplest OpenERP / odoo module:
from osv import osv
class my_custom_module(osv.osv):
_inherit = 'sale.order'
_name = 'sale.order'
def create(self, cr, uid, vals, ctx={}):
<your code here, whatever you want to do when new sale.order obejcet is created >
return super(my_custom_module, self).create(cr, uid, vals, ctx)
my_custom_module()
Does this help?
In a live commercial app. The method I use is a creation and update date/time stamp column.
Example
create table foo (
foo_id serial not null unique,
created_timestamp timestamp not null default current_timestamp,
updated_timestamp timestamp not null default current_timestamp
) with oids;
The trigger to do the work
CREATE TRIGGER check_table_to_do_update
--What i want to do
The trigger on the table
CREATE TRIGGER check_update_foo
AFTER UPDATE ON foo
FOR EACH ROW
WHEN (OLD.updated_timestamp IS DISTINCT FROM NEW.updated_timestamp)
EXECUTE PROCEDURE check_table__to_do_update();
Anything else can put an unnecessary overhead on the system.
All the best

android sqlite have gap after delete row?

I have 4 EditTexts in dataBase
I delete the second one
The database is numbered 1,3,4
I now want it to be renumbered as 1,2,3.
I looked at many solutions
But I could not find a complete answer..
I do not know what should be in writing
I tried the following code .
But it did not happen
db.delete("SQLITE_SEQUENCE","NAME = ?",new String[]{TABLE_NAME});
Does anyone have the full answer ?
In general, this is poor design for a database. An ID should be consistent for a piece of data, regardless of its peers--it should not fluctuate just because another row was deleted.
That being said, you could always use an UPDATE statement to SET ID = ID-1 WHERE ID > (the ID of the row you deleted). This would work. I don't recommend this approach, but you can.
You can recover this space by executing the SQLite VACUUM command.
Call db.execSQL("VACUUM") after delete

Android database search data on EditText TextChange

I am building an android application with a database that contains more than 20,000 entries.
When i retrieve data from the database, especially when searching for data, it seems to be working slow.
Especially, when i search data based on an editText. Every time editText TextChange(), i query :
Select * from mytable where data='mydata'
And it runs slowly.
I really don't know how to make it work faster.
Hope anyone can help me!
You can create an index on just that column to make selection work faster. If your column is the second one in an existing index, this index cannot be used most efficiently.
CREATE INDEX idx_mytable_word ON mytable(word)
May be you initialize db connection every time?
Try to add pagination. it's make your app faster.

SQLite in Android: is there a way to use a limited number of rows in my application, or should I remove them by hand?

I am currently building a database recording events on the phone, but as I don't want to make this a huge database, 100 events are more than enough.
This will keep my database light en efficient.
Unfortunately, I don't see a way to limit the number of rows other than
String sql = "DELETE FROM myTable WHERE _id <= "+limitId;
and I could run this code when the user launch/leaver the app, but I am expecting a better way to achieve this
Is there a more convenient way to achieve this?
If you are using a ContentProvider, you can implement your DELETE in onInsert, deleting a single row on every insert of a single row:
String sql = "DELETE FROM myTable WHERE _id IN (SELECT min(_id) FROM myTable)";
I guess you mean limit to the 100 newest events ?
if so, there is no better way to do it as you did: checking for entries on every insert and delete old entries if necessary.
It's just a matter of taste how or where you do your check, as flx mentioned you could do it in the ContentProvider or as you probably did in the BroadcastReceiver or Service where you actually add the new row. You could also set up a Trigger on your table, but the main idea remains the same. Here a link if you're interested in triggers:
http://www.tutorialspoint.com/sqlite/sqlite_triggers.htm

Android Widgets: Where would the 'insert' step for a database occur?

I have a widget that currently takes a random string from an array and sets it to text view on update. The issue here is that the same item can be re-used multiple times in a row due to the string being 'random'
In order to solve this I was going to create a table that held String text, and int viewednum and increment the viewed number each time 'get text' was called. (on update in the widget).
My Question: If I put the insert statements in the widget, won't the data be inserted every time 'on update' is called?
Would it be better for it to go in the DBadapter class somewhere? I'm just unsure about the best way to make sure I don't enter duplicate data. If there is a better alternative like saving a csv file somewhere and using that I'm open to it, it seemed like a sqlite database was the way to go.
Thank you for your time.
That depends on what your onUpdate method does. If each time onUpdate is called it gets a random String from the database, then that would be the place to put it. However, if you are not getting the String during onUpdate, then you should put it in the method where you are accessing your database. I think your confusion is about the purpose of onUpdate. onUpdate doesn't get called every time the user scrolls by the homepage and sees your widget; it gets called regularly on a timescale you specify, and the whole purpose of it is, in a case like yours, to get a new String from the database.
As for your second question, yes, SQlite databases are the way to do it :) I haven't tried saving a csv file or something like that, but I imagine that would be a lot more complex than just using a database.
Declare your database with a UNIQUE constraint on the columns you want to keep unique, then set the desired behaviour via ON CONFLICT in the INSERT statement. ON CONFLICT REPLACE... means the most recent INSERT overwrites. ON CONFLICT IGNORE... keeps the older version.

Categories

Resources