Room:不确定如何将 Cursor 转换为此方法的返回类型
Posted
技术标签:
【中文标题】Room:不确定如何将 Cursor 转换为此方法的返回类型【英文标题】:Room: Not sure how to convert a Cursor to this method's return type 【发布时间】:2019-12-12 21:09:20 【问题描述】:我无法在我的 Dao 中使用 LiveData。删除 LiveData 解决了编译错误。代码如下:
我查看了我的 Gradle 构建文件。我没有使用任何旧的支持库。我的代码也没有其他一般错误。
基类:
@Entity
@JsonClass(generateAdapter = false)
abstract class BaseEntity(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id")
var id: Long = 0L,
@ColumnInfo(name = "_created_at")
@Json(name = "created_at")
var createdAt: Date = Date(),
@ColumnInfo(name = "_updated_at")
@Json(name = "updated_at")
var updatedAt: Date = Date(),
@ColumnInfo(name = "_created_by")
@Json(name = "created_by")
var createdBy: String = ""
)
init
val applicationContext = BaseApp.applicationContext() as BaseApp
val username = applicationContext.sharedPreference?.getString(
applicationContext.getString(R.string.setting_login_username),
""
)
if (username != null)
createdBy = username
@Entity
@JsonClass(generateAdapter = true)
open class Queue(
@NonNull
@ColumnInfo(name = "name")
@Json(name = "name")
var name: String = "",
@NonNull
@ColumnInfo(name = "queue_type")
@Json(name = "queue_type")
var queueType: QueueType? = null,
@NonNull
@ColumnInfo(name = "queue_status")
@Json(name = "queue_status")
var queueStatus: QueueStatus? = null,
@ColumnInfo(name = "server_response")
@Json(name = "server_response")
var serverResponse: String = ""
) : BaseEntity()
@Dao
interface QueueDao
@Query("SELECT * FROM Queue")
suspend fun getQueue(): LiveData<List<Queue>>
Complete gradle build file:
===========================
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android
compileSdkVersion 28
defaultConfig
applicationId "____"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions
annotationProcessorOptions
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
compileOptions
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
dependencies
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'org.jetbrains.kotlin:kotlin-reflect:1.3.41'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha02'
// Room for database
def room_version = '2.1.0'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:2.1.0"
// moshi json parser
implementation("com.squareup.moshi:moshi-kotlin:1.8.0")
implementation("com.squareup.moshi:moshi-adapters:1.8.0")
kapt("com.squareup.moshi:moshi-kotlin-codegen:1.8.0")
// zxing-android-embedded
implementation('com.journeyapps:zxing-android-embedded:3.6.0')transitive = false
implementation 'com.google.zxing:core:3.3.3'
// kotlin coroutines and retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1'
implementation 'com.squareup.retrofit2:converter-moshi:2.5.0'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
// glide for image viewing/fetching
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
// dexter for permissions
implementation 'com.karumi:dexter:5.0.0'
// materialDrawer
implementation "com.mikepenz:materialdrawer:6.1.1"
// Google flexbox
implementation 'com.google.android:flexbox:1.1.0'
// UI Elements
implementation "androidx.constraintlayout:constraintlayout:1.1.3"
implementation "androidx.recyclerview:recyclerview:1.0.0"
implementation "com.google.android.material:material:1.0.0"
implementation "androidx.annotation:annotation:1.1.0"
// tests
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.LiveData<java.util.List<data.basequeue.Queue>>).
public abstract java.lang.Object getQueue(@org.jetbrains.annotations.NotNull()
只需从 Dao 中移除 LiveData 即可成功编译代码。
【问题讨论】:
【参考方案1】:Room 不支持具有暂停功能的 LiveData。此外,LiveData 已经在后台线程上工作,应该直接使用而不使用协程。
从我的代码中删除 suspend 解决了错误。
【讨论】:
以上是关于Room:不确定如何将 Cursor 转换为此方法的返回类型的主要内容,如果未能解决你的问题,请参考以下文章
Android 房间数据库 - 不确定如何将 Cursor 转换为此方法的返回类型
如何在 ROOM android 中修复“不确定如何将光标转换为此方法的返回类型”