How can a Room @Insert method return the saved object required by an existing Kotlin interface?


I am developing an Android expense-tracking application using Kotlin and Jetpack Compose. My project uses the gastos-core:1.1.0 library, which defines a GastoRepository interface.

Its agregar method returns a Gasto object:

suspend fun agregar(gasto: Gasto): Gasto

I am adding Room to persist expenses locally. My entity uses an auto-generated primary key:

@Entity(tableName = "gastos") data class GastoEntity( @PrimaryKey(autoGenerate = true) val id: Int = 0, val descripcion: String, val monto: Double, val categoria: String, val fecha: String )

My DAO insert method is currently declared as:

@Insert suspend fun insertar(gasto: GastoEntity)

The repository must implement agregar and return a Gasto, but the DAO method above does not return the generated database ID.

How should I implement this operation so that Room inserts the entity, retrieves the generated ID, and returns the corresponding Gasto object while preserving the existing repository interface?

0
Oct 28 at 5:04 PM
User AvatarGERSON DAVID MONTUFAR ZECEÑA
#android#kotlin#android-room

No answer found for this question yet.