Search code examples
javaandroidandroid-room

Room error: Not sure how to handle insert method's return type


I don't get it, I though this is the way to get the id of newly inserted row.

DAO

@Dao
public interface AlarmDao {

    .....

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    long insertAll(AlarmEntity...alarms); //used long instead of void
}

ENTITY

@Entity(tableName = "tb_alarm")
public class AlarmEntity {

    @PrimaryKey(autoGenerate = true)
    private long id;

    ...

    public long getId(){
        return this.id;
    }
}

but building is failed and I'm getting error which is pointing into my Dao class and the error is:

error: Not sure how to handle insert method's return type.

What did I missed about it?


Solution

  • AlarmEntity...alarms
    

    this translates in multiple inserts. So the return type should be a List<Long> or a long[], and it makes sense. If you pass two items you will get two id, one for each newly inserted row.

    If you want to insert only 1 item at time, remove the varargs (...). EG

    @Insert
    long insert(AlarmEntity alarms);