房间数据库:找不到符号变量 _ 结果

RoomDatabase: cannot find symbole variable _result

我正在尝试使用 @RawQuery 和 SupportSQLiteQuery 对我的 RoomDatabase 进行研究。 我有这个错误:"cannot fin symbole variable _result" 在 PropertyDao_Impl 中构建时。我已经多次尝试 CleanProject 和 Rebuild。

你知道我做错了什么吗? 提前致谢!

import androidx.lifecycle.MutableLiveData
import androidx.room.*
import androidx.sqlite.db.SupportSQLiteQuery
import com.openclassrooms.realestatemanager.add_edit.Property


@Dao
interface PropertyDao {

    @Query("SELECT * FROM Property")
    fun getAllProperties(): LiveData<List<Property>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun addProperty(property: Property): Long

    @Query("SELECT * FROM Property WHERE id_property = :id_property")
    suspend fun getPropertyFromId(id_property: String): Property

    @RawQuery(observedEntities = [Property::class])
    fun searchInDatabase(query: SupportSQLiteQuery): MutableLiveData<List<Property>>
}

PropertyDao_Impl.java(生成):

@Override
  public MutableLiveData<List<Property>> searchInDatabase(final SupportSQLiteQuery query) {
    final SupportSQLiteQuery _internalQuery = query;
    __db.assertNotSuspendingTransaction();
    final Cursor _cursor = DBUtil.query(__db, _internalQuery, false, null);
    try {
      return _result; // error here
    } finally {
      _cursor.close();
    }
  }
}
implementation "androidx.room:room-runtime:2.2.4"
kapt "androidx.room:room-compiler:2.2.4"

替换

@RawQuery(observedEntities = [Property::class])
fun searchInDatabase(query: SupportSQLiteQuery): MutableLiveData<List<Property>>

@RawQuery(observedEntities = [Property::class])
fun searchInDatabase(query: SupportSQLiteQuery): LiveData<List<Property>>

简而言之,如果您使用的是 Jetpack Paging 库 v3.0.0+,您也必须更新您的 Room 版本,v2.3.0-rc01 对我有用。

长答案。 当我尝试使用 Paging librar v3.0.0-beta03 & Room v2.2.6 时,这发生在我身上。

RoomEntityDAO

@RawQuery(observedEntities = [RoomEntity::class])
fun getOrdersDataSource(query: SupportSQLiteQuery): DataSource.Factory<Int, RoomEntity>

在 RoomEntityDAO_Impl

中生成
@Override
  public PagingSource<Integer, RoomEntity> getOrdersDataSource(
      final SupportSQLiteQuery query) {
    final SupportSQLiteQuery _internalQuery = query;
    __db.assertNotSuspendingTransaction();
    final Cursor _cursor = DBUtil.query(__db, _internalQuery, false, null);
    try {
      return _result;
    } finally {
      _cursor.close();
    }
  }

这个问题已经有一个公认的答案,但它可能会帮助其他有同样问题的人。