Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: method execute in class CoroutinesRoom cannot be applied to given types

I am using coroutines with room database and while fetching data from room I am getting below error

\app\build\generated\source\kapt\debug\co\location\locationapp\data\source\local\LocationDataDao_Impl.java:87: error: method execute in class CoroutinesRoom cannot be applied to given types; return CoroutinesRoom.execute(__db, true, new Callable() { ^ required: RoomDatabase,Callable,Continuation found: RoomDatabase,boolean,>,Continuation
reason: cannot infer type-variable(s) R (actual and formal argument lists differ in length) where R is a type-variable: R extends Object declared in method execute(RoomDatabase,Callable,Continuation) where CAP#1 is a fresh type-variable: CAP#1 extends Object super: Unit from capture of ? super Unit

Below is my Dao class

@Dao
interface LocationDataDao {

    @Query("SELECT * FROM location limit :limit offset :offset")
    suspend fun queryLocationData(limit: Int, offset: Int): List<Location>

    @Query("DELETE FROM location")
    suspend fun deleteAllLocationData(): Int

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insertAllLocationData(locationData: List<Location>)

}

Below is my repository class

class LocationRepository @Inject
constructor(private val apiInterface: ApiInterface, private val locationDataDao: LocationDataDao) {

    suspend fun getLocationDataFromApi(limit: Int, offset: Int): List<Location> {
        try {
            return apiInterface.getLocationData(offset, limit).await()
        } catch (ex: HttpException) {
            throw ApiError(ex)
        }
    }

    suspend fun getLocationDataFromDb(limit: Int, offset: Int): List<Location> {
        return locationDataDao.queryLocationData(limit, offset)
    }


    suspend fun insertData(locationList: List<Location>) {
        locationDataDao.insertAllLocationData(locationList)
    }

    suspend fun deleteDataFromDB() {
        locationDataDao.deleteAllLocationData()
    }

}

I am fetching data from a method like below

 fun loadAfter() {
    Job job = CoroutineScope(Dispatchers.IO).launch { 
   val data = locationRepository.getLocationDataFromDb(BuildConfig.PAGE_SIZE, params.key)
            }    
    }

Please let me know if further info is required

like image 376
Tarun Avatar asked Aug 22 '19 05:08

Tarun


1 Answers

Make sure that all room libraries have the same version in your app build.gradle:

...
dependencies {
  ...
  def room_version = "2.2.0-alpha02"
  implementation "androidx.room:room-runtime:$room_version"
  implementation "androidx.room:room-ktx:$room_version"
  kapt "androidx.room:room-compiler:$room_version"
}

See the Declaring dependencies documentation section.

like image 115
Valeriy Katkov Avatar answered Dec 03 '22 18:12

Valeriy Katkov