The update operation will update the existing data in our database. What follows is its implementation:
val NOTE = object : Crud<Note> { ... override fun update(what: Note) = update(listOf(what)) override fun update(what: Collection<Note>): Int { val db = DbHelper(name, version).writableDatabase db.beginTransaction() var updated = 0 what.forEach { item -> val values = ContentValues() val table = DbHelper.TABLE_NOTES values.put(DbHelper.COLUMN_TITLE, item.title) values.put(DbHelper.COLUMN_MESSAGE, item.message) values.put(DbHelper.COLUMN_LOCATION_LATITUDE,
item.location.latitude) values.put(DbHelper.COLUMN_LOCATION_LONGITUDE,
item.location.longitude) db.update(table, values, "_id = ?",
arrayOf(item.id.toString())) updated++ } val result = updated == what.size if (result) { db.setTransactionSuccessful() } else { updated = 0 } db.endTransaction() db.close() return updated } ... } ... val TODO = object : Crud<Todo> { ... override fun update(what: Todo) = update(listOf(what)) override fun update(what: Collection<Todo>): Int { val db = DbHelper(name, version).writableDatabase db.beginTransaction() var updated = 0 what.forEach { item -> val table = DbHelper.TABLE_TODOS val values = ContentValues() values.put(DbHelper.COLUMN_TITLE, item.title) values.put(DbHelper.COLUMN_MESSAGE, item.message) values.put(DbHelper.COLUMN_LOCATION_LATITUDE,
item.location.latitude) values.put(DbHelper.COLUMN_LOCATION_LONGITUDE,
item.location.longitude) values.put(DbHelper.COLUMN_SCHEDULED, item.scheduledFor) db.update(table, values, "_id = ?",
arrayOf(item.id.toString())) updated++ } val result = updated == what.size if (result) { db.setTransactionSuccessful() } else { updated = 0 } db.endTransaction() db.close() return updated } ... } ...