Remove All Ads from XDA
Honor 9 Lite

[Q] Implementing Android REST client and synchronization

1 posts
Thanks Meter: 0
By Edisoni, Junior Member on 4th July 2014, 10:05 AM
Post Reply Email Thread
Good afternoon. There is a task to develop a client for android news site. For notification of new posts to be used gcm.

Estimated algorithm:

1. Run the application, the database is empty, make a request to the api get the last 20 records, id 1000 to 980, write to the database.
3. Listview is scrolled to the last record, make a request for records whose id is less than 980, we record from 979 to 959 write to the database.
3. Comes gcm message or clicked update, make a request to the api, get the records whose id> 1000, we record with id 1001 to 1021 write to the database.

The question arises - how to behave in a situation where we assume the device was offline for a week, during which added another 1000 records.
The device connects to the network, gets send-to-sync message, makes a request to the api to get records whose id> 1021, in response to the 1000 records to come.

Intuition suggests that load all 1000 records are not correct, you need to download and say the last 20, but then it turns out that the database will be news for today, last week they will not, and then go for the news before last week

And question number 2 - how to clean old records, to avoid infinite growth of the DB?
4th July 2014, 10:29 AM |#2  
Junior Member
Flag Wrocław
Thanks Meter: 1
Donate to Me
What about this: make another table that have only 20 records and when a news comes, it stores it on the first record and deletes last record. Of course records 2-19 iterate.

When you get news on device, get them from this table. If you want to get older news, get the last id from this table and get next 20 news from table that stores all of the records.

---------- Post added at 10:29 AM ---------- Previous post was at 10:20 AM ----------

Answering question 2:

The best solution for me is to make model class News . It makes everything easier with DB. If you have this, you can do something like this:

public void deleteAllNews(){

String selectQuery = "SELECT * FROM " + TABLE_NEWS;

SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery(selectQuery, null);

// looping through all rows
if (c.moveToFirst()) {
do {

db.delete(TABLE_NEWS, KEY_ID + " = ?", new String[] { String.valueOf(c.getInt(c.getColumnIndex(KEY_ID)) });

} while (c.moveToNext());

Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes