【问题标题】:error: method execute in class CoroutinesRoom cannot be applied to given types错误:类 CoroutinesRoom 中的方法执行不能应用于给定类型
【发布时间】:2019-08-22 05:17:54
【问题描述】:

我在房间数据库中使用协程,在从房间获取数据时出现错误

\app\build\generated\source\kapt\debug\co\location\locationapp\data\source\local\LocationDataDao_Impl.java:87: 错误:无法将类 CoroutinesRoom 中的方法执行应用于 给定类型; return CoroutinesRoom.execute(__db, true, new Callable() { ^ 必需:RoomDatabase,Callable,Continuation 找到: RoomDatabase,boolean,>,Continuation
原因:无法推断类型变量 R (实际参数列表和形式参数列表的长度不同)其中 R 是类型变量: R扩展了在方法execute(RoomDatabase,Callable,Continuation)中声明的对象,其中 CAP#1 是一个新的类型变量: CAP#1 扩展 Object super: Unit from capture of ?超级单位

下面是我的道课

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

}

下面是我的存储库类

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

}

我正在从以下方法获取数据

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

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

【问题讨论】:

  • 不确定是否可以插入列表。你应该插入对象并用for循环或其他东西插入它

标签: kotlin android-room kotlin-coroutines


【解决方案1】:

确保所有房间库在您的应用 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"
}

请参阅Declaring dependencies 文档部分。

【讨论】:

  • 为我解决了。谢谢!
【解决方案2】:

确保升级到最新版本。 我是从2.2.6 to 2.3.0升级的,问题已经解决了。

def room_version = "2.3.0"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"

【讨论】:

    猜你喜欢
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多