Why are these RawContact deletions failing? #224
-
Use this code to delete raw contacts, it returns true.
Then use this code to query all raw contacts
Returned results still contain the raw contacts deleted. |
Beta Was this translation helpful? Give feedback.
Replies: 19 comments 5 replies
-
@yuanhoujun, this sounds like a duplicate of issue #172 that has been fixed since version 0.2.0. What version of the library are you using? If you are not using 0.2.0 or above, could you please upgrade and let me know if the issue persists? |
Beta Was this translation helpful? Give feedback.
-
@yuanhoujun, also looking at your code, I see that there are some improvements that can be made 😁 Instead of, fun getAllContacts(context: Context): List<ContactBasicInfo> {
val contacts =
Contacts(context).query().orderBy(ContactsFields.DisplayNamePrimary.asc()).find()
val rawContacts = mutableListOf<ContactBasicInfo>()
contacts.forEach { contact ->
contact.rawContacts.forEach { rawContact ->
val contactBasicInfo = ContactBasicInfo(
id = rawContact.id,
contactId = contact.id,
displayNamePrimary = rawContact.name?.displayName,
phoneNumber = rawContact.phones.sortedByDescending { it.isSuperPrimary }
.sortedByDescending { it.isPrimary }
.map { it.number }
.joinToString(separator = ",")
)
rawContacts.add(contactBasicInfo)
}
}
return rawContacts
} You could just use a flat map, fun getAllContacts(context: Context): List<ContactBasicInfo> = Contacts(context)
.query()
.orderBy(ContactsFields.DisplayNamePrimary.asc())
.find()
.flatMap { it.rawContacts }
.map { rawContact ->
ContactBasicInfo(
id = rawContact.id,
contactId = rawContact.contactId,
displayNamePrimary = rawContact.name?.displayName,
phoneNumber = rawContact.phones.sortedByDescending { it.isSuperPrimary }
.sortedByDescending { it.isPrimary }
.map { it.number }
.joinToString(separator = ",")
)
} |
Beta Was this translation helpful? Give feedback.
-
0.2.1 |
Beta Was this translation helpful? Give feedback.
-
Yep, thank you for your advice. |
Beta Was this translation helpful? Give feedback.
-
I was unable to reproduce the issue on my end just now. I'll have to investigate more tomorrow. Could you please share some logs with me? When you create your Contacts(context, logger = AndroidLogger(redactMessages = true)) Filter the LogCat for "ContactsDebug". Please copy-paste the log output here so I can see a bit more details 🙏 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@yuanhoujun, I also just noticed something that can be improved by the library thanks to your code sample! val contacts = Contacts(mContext)
val blankRawContacts = contacts.accounts().queryRawContacts().where { RawContactsFields.Id `in` request.ids }.find()
val rawContacts = blankRawContacts.mapNotNull { it.toRawContact(contacts) }
val isSuccessful = contacts.delete().rawContacts(rawContacts).commit().isSuccessful I created an issue (#220) that will simplify this to, val contacts = Contacts(mContext)
val blankRawContacts = contacts.accounts().queryRawContacts().where { RawContactsFields.Id `in` request.ids }.find()
val isSuccessful = contacts.delete().rawContacts(blankRawContacts).commit().isSuccessful That will remove one database query per I will also work on an existing issue (#158) that I have been planning to work on that will simplify this even more to, val isSuccessful = Contacts(mContext).delete().rawContactsWhere { RawContact.Id `in` request.ids }.commit().isSuccessful UPDATE (June 14): I also created another issue (#222), that does not require using WHERE. Usage would look like; val isSuccessful = Contacts(mContext).delete().rawContacts(request.ids).commit().isSuccessful |
Beta Was this translation helpful? Give feedback.
-
Yep, I noticed #158, That'll be more natural and better performance. so when will you release this feature? |
Beta Was this translation helpful? Give feedback.
-
Because you are interested in it, I will aim to have #158, #220, #222, and this bug you created by end of this week. I will probably create another release just for these issues. So, it will be in 0.2.2 =) |
Beta Was this translation helpful? Give feedback.
-
Ok, wonderful, 👍, thanks for your hard work. |
Beta Was this translation helpful? Give feedback.
-
I'll look at these logs tomorrow. @yuanhoujun, could you also please provide me with even more logs? It would help me a lot to see the state of the RawContacts table before and after you commit the delete operation. Please temporarily install the implementation 'com.github.vestrel00.contacts-android:debug:0.2.1' Then, log the RawContacts table before and after the delete operation. Also, please log the rawContactIds that should be deleted. So, something like; Log.d("ContactsDebug", "DELETING RAW CONTACTS WITH IDS: ${request.ids}")
mContext.logRawContactsTable()
val contacts = Contacts(mContext)
val blankRawContacts = contacts.accounts().queryRawContacts().where { RawContactsFields.Id `in` request.ids }.find()
val rawContacts = blankRawContacts.mapNotNull { it.toRawContact(contacts) }
val isSuccessful = contacts.delete().rawContacts(rawContacts).commit().isSuccessful
mContext.logRawContactsTable() Filter the LogCat with "ContactsDebug" and paste the output here. Please be careful NOT to paste sensitive personal information such as RawContact names. I advise you to use fake test data. |
Beta Was this translation helpful? Give feedback.
-
It's really personal, Could you add me on WeChat or Telegram? I can post these logs for you. I'm not sure this problem will appear if I use fake test data or another phone. |
Beta Was this translation helpful? Give feedback.
-
My telegram account: ouyangfeng2016, Wechat: ouyangfeng_office |
Beta Was this translation helpful? Give feedback.
-
It's not necessary to WeChat or Telegram yet, but I may want to if I can't figure this out tomorrow. I really only need to answer these questions.
Could you do what I suggested in #219 (comment) but instead of pasting the entire RawContacts table, you would just look at it yourself and give me the answers to my two questions above? All you need to do is check if the RawContacts row with |
Beta Was this translation helpful? Give feedback.
-
Try this, I removed some sensitive words. |
Beta Was this translation helpful? Give feedback.
-
@yuanhoujun, based on the logs. I see that the RawContact you are attempting to delete is NOT getting deleted! It is also not even being marked for deletion. I think I know what's going on here. I noticed that the RawContact you are trying to delete belongs to a I was going to ask you to log the value of If it is read-only, then we might not be able to delete it at all. The only way these RawContacts can be deleted is from the remote server/service that belongs to the BUT before we come to any conclusions, could you try deleting this Contact/RawContact using a different Contacts app? I'm not sure what Contacts apps are available in your region. I typically use the native AOSP Contacts app that comes pre-installed with most US Android phones. I also use;
Let me know if you are able to delete the Contact/RawContact using any of those apps (or similar apps) 🙏 I did verify that there is a bug in the delete APIs. The The best that I might be able to do for this thread is to fix the issue ^ so that I am willing to help do more but I will wait until you try deleting Contact/RawContact using a different Contacts app. I'll convert this issue to a discussion for now. |
Beta Was this translation helpful? Give feedback.
-
Your guess is right. But I think you need to return false when failed, like #223, go ahead. |
Beta Was this translation helpful? Give feedback.
-
Thanks for checking!
Yes. I have already fixed that issue. I'm just working on other tickets I'd like to include in the next release (0.2.2). One of those issues will also benefit you (#222). The release should be public before end of Friday this week.
Unfortunately, I am unable to add a read-only flag. As I mentioned, including |
Beta Was this translation helpful? Give feedback.
-
A new issue popped up that seems related to this; #293 |
Beta Was this translation helpful? Give feedback.
@yuanhoujun, based on the logs. I see that the RawContact you are attempting to delete is NOT getting deleted! It is also not even being marked for deletion. I think I know what's going on here.
I noticed that the RawContact you are trying to delete belongs to a
com.xiaomi
account. I think that this is a read-only RawContact; https://developer.android.com/reference/android/provider/ContactsContract.RawContactsColumns#RAW_CONTACT_IS_READ_ONLYI was going to ask you to log the value of
ContactsContract.RawContacts.RAW_CONTACT_IS_READ_ONLY
in the RawContacts table but the query keeps throwing an exception when adding that column 😬If it is read-only, then we might not be able to delete it a…