From 7c2345dac963aabacf40b519792faec55d77744a Mon Sep 17 00:00:00 2001 From: Matt Davies Date: Mon, 14 Nov 2016 22:18:22 +0800 Subject: [PATCH] Run getAll() in a background thread on Android to reduce performance impact --- .../reactnativecontacts/ContactsManager.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java b/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java index 3cc2a108..752024ab 100644 --- a/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java +++ b/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java @@ -4,6 +4,7 @@ import android.content.ContentResolver; import android.content.Context; import android.content.pm.PackageManager; +import android.os.AsyncTask; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds; import android.provider.ContactsContract.CommonDataKinds.StructuredName; @@ -30,14 +31,19 @@ public ContactsManager(ReactApplicationContext reactContext) { * queries CommonDataKinds.Contactables to get phones and emails */ @ReactMethod - public void getAll(Callback callback) { - Context context = getReactApplicationContext(); - ContentResolver cr = context.getContentResolver(); + public void getAll(final Callback callback) { + AsyncTask.execute(new Runnable() { + @Override + public void run() { + Context context = getReactApplicationContext(); + ContentResolver cr = context.getContentResolver(); - ContactsProvider contactsProvider = new ContactsProvider(cr, context); - WritableArray contacts = contactsProvider.getContacts(); + ContactsProvider contactsProvider = new ContactsProvider(cr, context); + WritableArray contacts = contactsProvider.getContacts(); - callback.invoke(null, contacts); + callback.invoke(null, contacts); + } + }); } /*