From 4fde91ae47b8ddf1efe109d5561f50bcf09f7acf Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 8 Apr 2018 13:03:56 +0200 Subject: [PATCH] limit batch group at adding/removing contacts from groups --- .../contacts/activities/GroupContactsActivity.kt | 10 ++++++---- .../contacts/helpers/ContactsHelper.kt | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt index 99c787b4..b32a0e1e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt @@ -109,9 +109,11 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh } override fun removeFromGroup(contacts: ArrayList) { - removeContactsFromGroup(contacts, group.id) - if (groupContacts.size == 0) { - refreshContacts() - } + Thread { + removeContactsFromGroup(contacts, group.id) + if (groupContacts.size == 0) { + refreshContacts() + } + }.start() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index 410cf2d5..c7130a79 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -24,6 +24,7 @@ import com.simplemobiletools.contacts.extensions.* import com.simplemobiletools.contacts.models.* class ContactsHelper(val activity: BaseSimpleActivity) { + private val BATCH_SIZE = 100 fun getContacts(callback: (ArrayList) -> Unit) { Thread { val contacts = SparseArray() @@ -886,6 +887,11 @@ class ContactsHelper(val activity: BaseSimpleActivity) { withValue(CommonDataKinds.GroupMembership.GROUP_ROW_ID, groupId) operations.add(build()) } + + if (operations.size % BATCH_SIZE == 0) { + activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) + operations.clear() + } } activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) } @@ -899,6 +905,11 @@ class ContactsHelper(val activity: BaseSimpleActivity) { withSelection(selection, selectionArgs) operations.add(build()) } + + if (operations.size % BATCH_SIZE == 0) { + activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) + operations.clear() + } } activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) } @@ -1117,7 +1128,6 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } private fun toggleFavorites(contacts: ArrayList, addToFavorites: Boolean) { - val applyBatchSize = 100 try { val operations = ArrayList() contacts.filter { it.source != SMT_PRIVATE }.map { it.contactId.toString() }.forEach { @@ -1127,7 +1137,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) { operations.add(build()) } - if (operations.size % applyBatchSize == 0) { + if (operations.size % BATCH_SIZE == 0) { activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) operations.clear() }