Post Reply

[Q] Implementing Android REST client and synchronization

4th July 2014, 09:05 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
1 posts
Join Date:Joined: Jul 2014
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, 09:29 AM   |  #2  
Junior Member
Flag Wrocław
Thanks Meter: 1
 
14 posts
Join Date:Joined: Jun 2014
Donate to Me
More
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());
}
db.close();
c.close();

}
Last edited by panwrona; 4th July 2014 at 09:50 AM.
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools
Display Modes


Top Threads in Java for Android App Development by ThreadRank