错误:无法将 CoroutinesRoom 类中的方法执行应用于给定类型

Tar*_*run 4 kotlin android-room kotlin-coroutines

我正在使用带有房间数据库的协程,并且在从房间获取数据时出现以下错误

\app\build\generated\source\kapt\debug\co\location\locationapp\data\source\local\LocationDataDao_Impl.java:87: 错误:CoroutinesRoom 类中的方法执行不能应用于给定类型;return CoroutinesRoom.execute(__db, true, new Callable() { ^ required: RoomDatabase,Callable,Continuation found: RoomDatabase,boolean,>,Continuation
reason: cannot infer type-variable(s) R(实际和形式参数列表在length) 其中 R 是一个类型变量:R 扩展了在方法 execute(RoomDatabase,Callable,Continuation) 中声明的对象,其中 CAP#1 是一个新的类型变量:CAP#1 extends Object super:从捕获的 ? super Unit 的单元

下面是我的 Dao 类

@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>)

}
Run Code Online (Sandbox Code Playgroud)

下面是我的存储库类

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()
    }

}
Run Code Online (Sandbox Code Playgroud)

我正在从如下方法中获取数据

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

如果需要更多信息,请告诉我

Val*_*kov 13

确保所有房间库在您的应用 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"
}
Run Code Online (Sandbox Code Playgroud)

请参阅声明依赖项文档部分。

  • 为我解决了。谢谢! (2认同)