Improved performance when inserting new feeds into the database

This commit is contained in:
daniel oeh 2012-07-06 17:03:06 +02:00
parent ac755c8899
commit ebb9e0b30c
3 changed files with 42 additions and 16 deletions

View File

@ -66,7 +66,11 @@ public class AddFeedActivity extends SherlockActivity {
requester.cancelDownload(getContext(), downloadId);
}
try {
unregisterReceiver(downloadCompleted);
} catch (IllegalArgumentException e) {
// ignore
}
dismiss();
}
@ -131,9 +135,12 @@ public class AddFeedActivity extends SherlockActivity {
long statusId = manager.addDownloadStatus(
AddFeedActivity.this, new DownloadStatus(
feed, reason, false));
Intent intent = new Intent(DownloadService.ACTION_DOWNLOAD_HANDLED);
intent.putExtra(DownloadService.EXTRA_DOWNLOAD_ID, downloadId);
intent.putExtra(DownloadService.EXTRA_STATUS_ID, statusId);
Intent intent = new Intent(
DownloadService.ACTION_DOWNLOAD_HANDLED);
intent.putExtra(DownloadService.EXTRA_DOWNLOAD_ID,
downloadId);
intent.putExtra(DownloadService.EXTRA_STATUS_ID,
statusId);
AddFeedActivity.this.sendBroadcast(intent);
}
});

View File

@ -145,6 +145,7 @@ public class FeedManager {
* instead of the setters of FeedItem.
*/
public void markItemRead(Context context, FeedItem item, boolean read) {
Log.d(TAG, "Setting item with title " + item.getTitle() + " as read/unread");
item.read = read;
setFeedItem(context, item);
if (read == true) {
@ -246,10 +247,7 @@ public class FeedManager {
Collections.sort(feeds, new FeedtitleComparator());
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
feed.setId(setFeed(feed, adapter));
for (FeedItem item : feed.getItems()) {
setFeedItem(item, adapter);
}
adapter.setCompleteFeed(feed);
adapter.close();
}
@ -266,7 +264,7 @@ public class FeedManager {
"Found no existing Feed with title " + newFeed.getTitle()
+ ". Adding as new one.");
// Add a new Feed
markItemRead(context, newFeed.getItems().get(0), false);
newFeed.getItems().get(0).read = false;
addNewFeed(context, newFeed);
return newFeed;
} else {
@ -275,8 +273,8 @@ public class FeedManager {
// Look for new or updated Items
for (int idx = 0; idx < newFeed.getItems().size(); idx++) {
FeedItem item = newFeed.getItems().get(idx);
FeedItem oldItem = searchFeedItemByLink(savedFeed,
item.getLink());
FeedItem oldItem = searchFeedItemByTitle(savedFeed,
item.getTitle());
if (oldItem == null) {
// item is new
item.setFeed(savedFeed);
@ -302,9 +300,9 @@ public class FeedManager {
}
/** Get a FeedItem by its link */
private FeedItem searchFeedItemByLink(Feed feed, String link) {
private FeedItem searchFeedItemByTitle(Feed feed, String title) {
for (FeedItem item : feed.getItems()) {
if (item.getLink().equals(link)) {
if (item.getTitle().equals(title)) {
return item;
}
}
@ -324,7 +322,7 @@ public class FeedManager {
/** Updates Information of an existing Feeditem. Uses external adapter. */
public long setFeedItem(FeedItem item, PodDBAdapter adapter) {
if (adapter != null) {
return adapter.setFeedItem(item);
return adapter.setSingleFeedItem(item);
} else {
Log.w(TAG, "Adapter in setFeedItem was null");
return 0;
@ -373,7 +371,7 @@ public class FeedManager {
public long setFeedItem(Context context, FeedItem item) {
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
long result = adapter.setFeedItem(item);
long result = adapter.setSingleFeedItem(item);
adapter.close();
return result;
}

View File

@ -253,12 +253,31 @@ public class PodDBAdapter {
return media.getId();
}
/** Insert all FeedItems of a feed and the feed object itself in a single transaction */
public void setCompleteFeed(Feed feed) {
db.beginTransaction();
setFeed(feed);
for (FeedItem item : feed.getItems()) {
setFeedItem(item);
}
db.setTransactionSuccessful();
db.endTransaction();
}
public long setSingleFeedItem(FeedItem item) {
db.beginTransaction();
long result = setFeedItem(item);
db.setTransactionSuccessful();
db.endTransaction();
return result;
}
/**
* Inserts or updates a feeditem entry
*
* @return the id of the entry
*/
public long setFeedItem(FeedItem item) {
private long setFeedItem(FeedItem item) {
ContentValues values = new ContentValues();
values.put(KEY_TITLE, item.getTitle());
values.put(KEY_LINK, item.getLink());
@ -279,8 +298,10 @@ public class PodDBAdapter {
values.put(KEY_READ, item.isRead());
if (item.getId() == 0) {
Log.d(TAG, "inserting new feeditem into db");
item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values));
} else {
Log.d(TAG, "updating existing feeditem in db");
db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?",
new String[] { String.valueOf(item.getId()) });
}