0
votes

I want to cache RSS feeds into SQLite database to improve performance of the app.
I am only concerned with the very basic of the RSS feed namely the title, description, URL guid and the pubDate. As of now, I can process them and store them into Vector<Feed> where Feed is my custom class.

There area few things which I find mind-boggling as a newbie to Android.
Say I have feed items whose titles are as below:

1. Cat Kills the Dog
2. Curosity Killed the Cat in Revenge
3. Y U NO leave Cat alone ?  

In the first run, getting the row count of the table will return zero which means that I have to add all the feeds into the database. No problems.
The problem comes when the feeds are updated. Here is the updated feed:

*. Quick Brown Fox Jumps Over The Lazy Dog. Dog Dies.
1. Cat Kills the Dog
2. Curosity Killed the Cat in Revenge
3. Y U NO leave Cat alone ?

The logic is:
1. Start parsing the RSS feeds.
2. Read the title of the newly fetched RSS feed.
3. Does it match the title of the first row of the data base?
3.1. No, then keep parsing and add this feed to the database.
3.2. Yes, then stop parsing and add this feed to the database.

Problem is, the row added now will be the LAST ROW of the table while it should be, as in the RSS XML feed, the first row so that subsequent comparison becomes easier.

The thing is to maintain the order of rows similar to that in the feed XML.
How do I maintain that order ?

Or, is there a better way to organize and store the feeds ?

1

1 Answers

1
votes

First off, do not rely on the titles being unique. Instead, use the guid field which is meant to be the unique identifier for the item (see the RSS specification).

Updating feed

Go through each item in the retrieved feed and see if the guid already exists in the database, if so, skip it.

If it does not exist, create an entry.

So, revising your logic:

  1. Start parsing the RSS feeds.
  2. Read the guid of the newly fetched RSS feed.
  3. Does it match the guid of any row in the database? 3.1. No, then keep parsing and add this feed to the database. 3.2. Yes, then stop parsing and add this feed to the database.

Order of the items

There is also a field called pubDate which indicates when the item was published. Simply store the pubDate for each item in the database and use it in an ORDER BY clause when retrieveing:

SELECT * FROM items ORDER BY pubDate DESC

That will give you the items in order of original publication.

UPDATE: Since SQLite does not have a native data type for dates, you could either utilize the time/date helper functions or convert pubDate to epoch format in your code before you insert it into the database.