From 2db8034c3168586d7629a81878f43ebd9e5bca9c Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 27 Nov 2012 16:46:59 +0100 Subject: [PATCH] Use one database transaction when doing bulk flag changes Previously we used one transaction per message. For 500 messages the database updates alone took over 30s on my Nexus 7. --- src/com/fsck/k9/mail/store/LocalStore.java | 25 +++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index 21f03472f..af6548915 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -2795,11 +2795,30 @@ public class LocalStore extends Store implements Serializable { } @Override - public void setFlags(Message[] messages, Flag[] flags, boolean value) + public void setFlags(final Message[] messages, final Flag[] flags, final boolean value) throws MessagingException { open(OpenMode.READ_WRITE); - for (Message message : messages) { - message.setFlags(flags, value); + + // Use one transaction to set all flags + try { + database.execute(true, new DbCallback() { + @Override + public Void doDbWork(final SQLiteDatabase db) throws WrappedException, + UnavailableStorageException { + + for (Message message : messages) { + try { + message.setFlags(flags, value); + } catch (MessagingException e) { + Log.e(K9.LOG_TAG, "Something went wrong while setting flag", e); + } + } + + return null; + } + }); + } catch (WrappedException e) { + throw(MessagingException) e.getCause(); } }