From 4cbe122672a702091d4f391c9a5aef3aae437fd2 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 6 Apr 2018 18:18:27 +0200 Subject: [PATCH] delete contacts on a background thread --- .../contacts/helpers/ContactsHelper.kt | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) 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 06c00a22..b2e02e13 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -1044,25 +1044,27 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } fun deleteContacts(contacts: ArrayList) { - val localContacts = contacts.filter { it.source == SMT_PRIVATE }.map { it.id.toString() }.toTypedArray() - activity.dbHelper.deleteContacts(localContacts) + Thread { + val localContacts = contacts.filter { it.source == SMT_PRIVATE }.map { it.id.toString() }.toTypedArray() + activity.dbHelper.deleteContacts(localContacts) - try { - val contactIDs = HashSet() - val operations = ArrayList() - val selection = "${ContactsContract.Data.CONTACT_ID} = ?" - contacts.filter { it.source != SMT_PRIVATE }.forEach { - ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply { - val selectionArgs = arrayOf(it.contactId.toString()) - withSelection(selection, selectionArgs) - operations.add(this.build()) + try { + val contactIDs = HashSet() + val operations = ArrayList() + val selection = "${ContactsContract.Data.CONTACT_ID} = ?" + contacts.filter { it.source != SMT_PRIVATE }.forEach { + ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply { + val selectionArgs = arrayOf(it.contactId.toString()) + withSelection(selection, selectionArgs) + operations.add(this.build()) + } + contactIDs.add(it.id.toString()) } - contactIDs.add(it.id.toString()) - } - activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) - } catch (e: Exception) { - activity.showErrorToast(e) - } + activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) + } catch (e: Exception) { + activity.showErrorToast(e) + } + }.start() } }