我有这个任务,我需要查询每个联系人的所有详细信息并进行备份。
前770个联系人需要5-6秒才能加载,而在770个联系人之后,每个联系人需要超过一秒的时间来查询所有数据。我知道这是一个复杂的查询,但这太慢了。有没有办法优化这个?
代码
// This contacts array typically contains more than a thousand items in it, even more
contacts.forEach {
val lookupKey = it.lookupKey
queryContacts(lookupKey)
}
fun queryContacts(lookupKey: String) {
val contact = Contact()
val contactsCursor = resolver.query(CONTENT_URI, arrayOf(_ID, DISPLAY_NAME, HAS_PHONE_NUMBER), CONTACTS_ALL_DETAILS_SELECTION, arrayOf(lookupKey), null)
if (contactsCursor.count > 0) {
while (contactsCursor.moveToNext()) {
val contactId = contactsCursor.getString(contactsCursor.getColumnIndex(_ID))
val name = contactsCursor.getString(contactsCursor.getColumnIndex(DISPLAY_NAME))
contact.name = name
val hasPhoneNumber = Integer.parseInt(contactsCursor.getString(contactsCursor.getColumnIndex(HAS_PHONE_NUMBER)))
// This is to read all phone numbers associated with the contact
val phoneNumbers = arrayListOf<String>()
if (hasPhoneNumber > 0) {
val phoneCursor = contentResolver.query(PHONE_CONTENT_URI, null, "$PHONE_CONTACT_ID = ?", arrayOf(contactId), null)
while (phoneCursor.moveToNext()) {
phoneNumbers.add(phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)))
}
phoneCursor.close()
}
contact.phoneNumbers.addAll(phoneNumbers)
// Read every email id associated with the contact
val emailCursor = contentResolver.query(EMAIL_CONTENT_URI, null, "$EMAIL_CONTACT_ID = ?", arrayOf(contactId), null)
val emailIds = arrayListOf<String>()
while (emailCursor.moveToNext()) {
val email = emailCursor.getString(emailCursor.getColumnIndex(DATA))
emailIds.add(email)
}
emailCursor.close()
contact.emails.addAll(emailIds)
val columns = arrayOf(
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.Event.TYPE,
ContactsContract.CommonDataKinds.Event.MIMETYPE)
val where = ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +
" and " + ContactsContract.CommonDataKinds.Event.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE + "' and " + ContactsContract.Data.CONTACT_ID + " = " + contactId
val selectionArgs = arrayOf<String>()
val sortOrder = ContactsContract.Contacts.DISPLAY_NAME
val birthdayCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, columns, where, selectionArgs, sortOrder);
val birthayList = arrayListOf<String>()
if (birthdayCur.count > 0) {
while (birthdayCur.moveToNext()) {
val birthday = birthdayCur.getString(birthdayCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE))
birthayList.add(birthday)
}
}
birthdayCur.close()
contact.dates.addAll(birthayList)
}
}
contactsToBackup.add(contact)
contactsCursor.close()
}
正如您所看到的,进行的整个复杂查询事件被调用超过1000次。我怎样才能改善这个?
答案 0 :(得分:1)
您真的不需要#!/bin/bash
MATCH='Text_To_Look_For'
INSERT='Text_To_Add'
FILE='/tmp/xxx'
echo "Modifying file..."
sed -i "/$MATCH/a $INSERT" $FILE
echo "Done."
您需要的是将数千个查询迁移到一个大查询中。
我假设这部分:
optimise thousands of SQLite queries
实际上在设备上的所有联系人上运行,如果是这样,你可以这样做:
contacts.forEach {
val lookupKey = it.lookupKey
queryContacts(lookupKey)
}