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:
- Start parsing the RSS feeds.
- Read the
guid
of the newly fetched RSS feed. - 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.