Room doesn't autogenerate Primary Key
Try making the id non-nullable:
@PrimaryKey(autoGenerate = true)
var wID: Long = 0
EDIT: I've found this in the sample code here. you can make your @Insert methods return the id of the inserted row object, so you could do this:
In your Dao:
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(weatherEntry: WeatherEntry) : Long
In your refresh data method:
private fun refreshData(location: String): Flowable<MutableList<WeatherEntry>> {
return remoteDataSource.getWeatherEntries(location,true).doOnNext({
// Clear cache
caches.clear()
// Clear data in local storage
localDataSource.deleteAllWeatherEntries()
}).flatMap(({ Flowable.fromIterable(it) })).doOnNext({ entry ->
val entryID = localDataSource.insertWeatherEntry(entry)
entry.wID = entryID
caches.add(entry)
}).toList().toFlowable()
}
Read https://developer.android.com/reference/androidx/room/PrimaryKey?hl=en#autoGenerate() , you'll get the answer
public boolean autoGenerate ()
Set to true to let SQLite generate the unique id.
When set to true, the SQLite type affinity for the field should be INTEGER.
If the field type is long or int (or its TypeConverter converts it to a long or int), Insert methods treat 0 as not-set while inserting the item.
If the field's type is Integer or Long (or its TypeConverter converts it to an Integer or a Long), Insert methods treat null as not-set while inserting the item.