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

View File

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

View File

@ -252,13 +252,32 @@ public class PodDBAdapter {
} }
return media.getId(); 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 * Inserts or updates a feeditem entry
* *
* @return the id of the entry * @return the id of the entry
*/ */
public long setFeedItem(FeedItem item) { private long setFeedItem(FeedItem item) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(KEY_TITLE, item.getTitle()); values.put(KEY_TITLE, item.getTitle());
values.put(KEY_LINK, item.getLink()); values.put(KEY_LINK, item.getLink());
@ -279,8 +298,10 @@ public class PodDBAdapter {
values.put(KEY_READ, item.isRead()); values.put(KEY_READ, item.isRead());
if (item.getId() == 0) { if (item.getId() == 0) {
Log.d(TAG, "inserting new feeditem into db");
item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values)); item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values));
} else { } else {
Log.d(TAG, "updating existing feeditem in db");
db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?",
new String[] { String.valueOf(item.getId()) }); new String[] { String.valueOf(item.getId()) });
} }