-
Testing on emulator. suspend fun createTemporalContact(temporalContact: NewTemporalContact): Boolean {
val rawContact: NewRawContact = NewRawContact().apply {
setName {
displayName = temporalContact.name
givenName = temporalContact.name
}
addPhone {
number = temporalContact.phone
}
}
val insertResult = contacts
.insertWithPermission()
.rawContact {
setName {
displayName = temporalContact.name
}
addPhone {
number = temporalContact.phone
}
}
.commit()
val contact = insertResult.contact(contacts, rawContact)
Log.i("AlormaContacts", "Contact: $contact") // <-- contact is NULL here
if (insertResult.isSuccessful && contact != null) {
val entity = TemporalContactEntity(
rawContactId = contact.id,
name = temporalContact.name,
phone = temporalContact.phone,
deleteAt = temporalContact.deleteAt,
)
temporalContactDao.insert(entity)
return true
}
return false
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey @alorma, I don’t think the bug is with the library so I changed this issue into a discussion. We can create another issue if there is indeed a bug with the library. I see that the issue is that you are creating an instance of a Try this, suspend fun createTemporalContact(temporalContact: NewTemporalContact): Boolean {
val rawContact: NewRawContact = NewRawContact().apply {
setName {
displayName = temporalContact.name
}
addPhone {
number = temporalContact.phone
}
}
val insertResult = contacts
.insertWithPermission()
.rawContacts(rawContact)
.commit()
val insertedRawContact = insertResult.rawContact(contacts, rawContact)
if (insertResult.isSuccessful && insertedRawContact != null) {
val entity = TemporalContactEntity(
rawContactId = insertedRawContact.id,
... I changed two things in the code snippet you provided,
For the second change, you can still retrieve the Also, it seems like you don't even need to retrieve the full contact or raw contact from the insert result. You just want the raw contact id, which you can get directly from the insert result. The shortest code for what you want to achieve is this... suspend fun createTemporalContact(temporalContact: NewTemporalContact): Boolean {
val insertResult = contacts
.insertWithPermission()
.rawContact {
setName {
displayName = temporalContact.name
}
addPhone {
number = temporalContact.phone
}
}
.commit()
if (insertResult.isSuccessful) {
val entity = TemporalContactEntity(
rawContactId = insertResult.rawContactIds.first(),
... Let me know if this fixes your issue. Full documentation on handling insert results can be found at https://vestrel00.github.io/contacts-android/basics/insert-contacts/#handling-the-insert-result |
Beta Was this translation helpful? Give feedback.
Hey @alorma, I don’t think the bug is with the library so I changed this issue into a discussion. We can create another issue if there is indeed a bug with the library.
I see that the issue is that you are creating an instance of a
NewRawContact
twice. The first instance you created is not passed into the insert function but is passed into the insert result.Try this,