tmath
tmath

Reputation: 574

Room database: inserted ID is always 0

I am trying to create a Room database of list items. I am following the example here: https://medium.com/mindorks/room-kotlin-android-architecture-components-71cad5a1bb35

Here is my data class:

@Entity(tableName = "itemData")
data class ItemData(@PrimaryKey(autoGenerate = true) var id: Long? = null,
                    @ColumnInfo(name = "text") var text: String,
                    @ColumnInfo(name = "checked") var checked: Boolean

)

The DAO:

@Dao
interface ItemDataDAO {

    @Insert(onConflict = REPLACE)
    fun insert(itemData: ItemData) : Long
}

The database:

@Database(entities = arrayOf(ItemData::class), version = 1)
abstract class ItemDatabase() : RoomDatabase() {


    abstract fun itemDataDao(): ItemDataDAO

    companion object {
        private var INSTANCE: ItemDatabase? = null

        fun getInstance(context: Context): ItemDatabase? {
            if (INSTANCE == null) {
                synchronized(ItemDatabase::class) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            ItemDatabase::class.java, "items.db")
                            .build()
                }
            }
            return INSTANCE
        }

        fun destroyInstance() {
            INSTANCE = null
        }
    }
}

Worker thread:

class DbWorkerThread(threadName: String) : HandlerThread(threadName) {

    private lateinit var mWorkerHandler: Handler

    override fun onLooperPrepared() {
        super.onLooperPrepared()
        mWorkerHandler = Handler(looper)
    }

    fun postTask(task: Runnable) {
        mWorkerHandler.post(task)
    }
}

Here is the listener than calls the insert function:

editText.setOnEditorActionListener { v, actionId, event ->
    return@setOnEditorActionListener when (actionId) {
        EditorInfo.IME_ACTION_DONE -> {
            createItem(editText)
            true
        }
        else -> false
    }
}

Here is the function that gets called to insert the item:

fun createItem(editText: EditText) {
    var text = editText.text.toString().trim()
    var itemData = ItemData(text = text, checked = false)
    var id : Long? = 0
    val task = Runnable {
        id = mDb?.itemDataDao()?.insert(itemData)
        println("inserted $id")
    }
    mDbWorkerThread.postTask(task)
}

But the ID is always 0 so only one item ever gets inserted. Can anyone see the problem?

I have also tried removing the default null value for the id, but it gives the same result.

Upvotes: 3

Views: 3785

Answers (3)

NELSON RODRIGUEZ
NELSON RODRIGUEZ

Reputation: 369

When create data class set the primary key to null, room autoincrement it!, they works to me!

Upvotes: 0

tmath
tmath

Reputation: 574

The following changes fixed the issue.

Instead of calling the constructor like this

var itemData = ItemData(text = text, checked = false)

I called the default constructor and then set the parameters after construction.

var itemData = ItemData()
itemData.text = text
itemData.checked = false

This required the following constructor to the ItemData entity:

constructor():this(null,"",false)

Upvotes: 2

Martin Zeitler
Martin Zeitler

Reputation: 76799

you'd to add an auto-increment column:

@Entity(tableName = "item_data")
public class ItemData extends BaseObservable {

    /** Fields */
    @ColumnInfo(name = "item_id")
    @PrimaryKey(autoGenerate = true)
    private int itemId;

    ...
}

Upvotes: 0

Related Questions