问题
I am trying to sync user phone numbers to firestore. It seems to work but when i delete a contact from my phone, it seems like some other contacts id's are being replaced, causing unnecessary deletion and creation to firestore. My only clue is that these contacts mostly around id 120,000. Is it normal? What is goung on?
This is how i get new created contacts, lcid is the last-contact-id registered to firestore:
private fun getNewContacts(): Cursor? {
val projection = arrayOf(
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER)
val selection = ContactsContract.Contacts._ID + "> ?"
val selectionArgs = arrayOf(mFireContactDetails!!.lcid.toString())
val sortOrder = ContactsContract.Contacts._ID + " ASC"
return mContentResolver.query(
ContactsContract.Contacts.CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder)
}
This is how i get the deleted contacts, ldel_ms is the last deleted timestamp registered to firestore:
private fun getDeletedContacts(): Cursor? {
val projection = arrayOf(
ContactsContract.DeletedContacts.CONTACT_ID,
ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP)
val selection = ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP + "> ?"
val selectionArgs = arrayOf(mFireContactDetails!!.ldel_ms.toString())
val sortOrder = ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP + " ASC"
return mContentResolver.query(
ContactsContract.DeletedContacts.CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder)
}
Next is a log example. When i delete a contact it is recognized as deleted but also other random contacts replacing their ID (i did not mention the log names)
values retrieval success
new contacts detected
adding 120797
adding 120803
adding 120804
adding 120805
adding 120806
adding 120807
adding 120808
adding 120809
adding 120810
sync new contacts success
deleted contacts detected
deleting contact id: 119576
deleting contact id: 120798
deleting contact id: 120799
deleting contact id: 120800
deleting contact id: 120801
deleting contact id: 120802
deleting contact id: 119762
deleting contact id: 119700
deleting contact id: 119561
deleting contact id: 119613
sync deleted contacts success
回答1:
From the documentations and articles it seems that android can sometimes change the contact id, this is very strange and un-expected but makes sense after my many weeks and attempts trying to solve this issue.
From what i can understand, the reasons are quite diverse so i can guess also in my case when one change or delete of contact can change other contact id's.
It seems that to overcome, i need to use the `LOOKUP_KEY, which i yet to understand how.
Some documentations includes:
Detecting changes in android contacts
Check if contact has been changes on...
LOOKUP_KEY
Hopes it helps since the android documentations are so hard to follow
来源:https://stackoverflow.com/questions/59791396/when-deleting-a-contact-on-android-other-random-contacts-ids-being-changed