I'm iterating through Contacts
table and each iteration do iteration on Phone
table so it takes appx. 4 seconds to do contact list for 243 contacts (without iterating phone numbers it loads instantly). I'm sure there is a way to make it faster.
Already made projections to fetch only needed columns, but haven't improved that much. Maybe I should improve SQLite query or iterate in some other manner:
Contact realmContact = new Contact();
Uri uri = Contacts.CONTENT_URI;
String[] projection = {
Contacts.LOOKUP_KEY,
Contacts.DISPLAY_NAME_PRIMARY,
Contacts.LAST_TIME_CONTACTED,
Contacts.HAS_PHONE_NUMBER
};
String selection = "((" + CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " != '' ))";
Cursor phones = getActivity()
.getContentResolver()
.query(uri, projection, selection, null, null);
while (phones.moveToNext()) {
String id = phones.getString(phones.getColumnIndex(Contacts.LOOKUP_KEY));
String name = phones.getString(phones.getColumnIndex(Contacts.DISPLAY_NAME_PRIMARY));
String lastTimeContacted = phones.getString(phones.getColumnIndex(Contacts.LAST_TIME_CONTACTED));
long data = Long.parseLong(lastTimeContacted);
String date = new SimpleDateFormat("dd/MM/yyyy | HH:mm").format(new Date(data));
if (Integer.parseInt(phones.getString(phones.getColumnIndex(Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = getActivity().getContentResolver().query(
CommonDataKinds.Phone.CONTENT_URI,
new String[]{CommonDataKinds.Phone.NUMBER},
CommonDataKinds.Phone.LOOKUP_KEY + " = ?",
new String[]{id}, null);
int iterationCounter = 0;
String phoneNumber = "";
while (pCur.moveToNext()) {
phoneNumber += iterationCounter == 0 ?
pCur.getString(pCur.getColumnIndex(CommonDataKinds.Phone.NUMBER))
: "," + pCur.getString(pCur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
iterationCounter += 1;
}
realmContact.setNumber(phoneNumber);
Log.i("asd-number", phoneNumber);
pCur.close();
}
realmContact.setId(id);
realmContact.setName(name);
realmContact.setLastTimeContacted(
getActivity().getResources()
.getString(R.string.contacts_list_fragment_last_call)
+ date);
realmContact.setIsBeingSaved(true);
_realm.insertOrUpdate(realmContact);