实际上我正在用Kotlin为Android Studio项目编写一个存储库模式。我有一个名为“BaseRepository”的通用接口。此接口从“BaseRealmRepository”和其他接口扩展而来。
我需要将Realm.db的所有结果都放到findAll()中,但是我无法修改这个继承的函数,因为它在其他存储库中是通用的。
当我尝试时,我收到以下错误:
“有趣的findAllRealm ( clazz:Class ) :列表 不满意:推断类型GenericRealmModelType< >不是RealmModel的子类型“*
但我真正的问题是类的clazz参数,我需要做什么演员或来传递到“where”clamule of realmQuery 而不修改泛型函数添加类似或类似的上限
这是我的通用界面:
interface BaseRepository<T> {
fun create(item: T) : T
fun remove(item: T)
fun update(item: T) : T
fun find(item: T): T
fun <T> findAll(clazz: Class<T>): List<T>
}
我为do cast创建了一个通用类型:
interface GenericRealmModelType<out T: RealmModel>{}
最后,我的抽象类“BaseObjectsRealmRepository”从“BaseRepository”扩展而来:
abstract class BaseObjectsRealmRepository<T> : BaseRepository<T> {
...
override fun <T> findAll(clazz: Class<T>): List<T> {
var klazz: Class<GenericRealmModelType<*>> = clazz as Class<GenericRealmModelType<*>>
val allResults = findAllRealm(klazz)
return allResults
}
private fun <T: RealmModel> findAllRealm(clazz: Class<T>): List<T>{
val realm = Realm.getDefaultInstance()
val realmResults = realm
.where(clazz) //CLAZZ IS MY PROBLEM
.findAll()
val allResults: List<T> = realm.copyFromRealm(realmResults)
return allResults
}
}
项目配置如下:
Project build.gradle:
buildscript {
ext.kotlin_version = '1.2.41'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "io.realm:realm-gradle-plugin:5.1.0"
classpath 'com.google.gms:google-services:3.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
模块app build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.example"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.2'
implementation 'com.google.dagger:dagger-android:2.12'
implementation 'com.google.dagger:dagger-android-support:2.12'
kapt 'com.google.dagger:dagger-android-processor:2.12'
kapt 'com.google.dagger:dagger-compiler:2.13'
implementation 'com.android.support:support-annotations:27.1.1'
implementation 'com.google.firebase:firebase-auth:15.1.0'
implementation 'com.google.android.gms:play-services-auth:15.0.1'
implementation 'com.google.firebase:firebase-core:15.0.2'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
}
realm {
syncEnabled = true
}
repositories {
mavenCentral()
}
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'
我该怎么办?
谢谢!
答案 0 :(得分:0)
我相信您可以通过完全删除interface GenericRealmModelType<out T: RealmModel>{}
来解决此问题。
abstract class BaseObjectsRealmRepository<T> : BaseRepository<T> {
...
override fun <T> findAll(clazz: Class<T>): List<T> =
Realm.getDefaultInstance().use { r ->
r.copyFromRealm(r.where(clazz).findAll())
}
答案 1 :(得分:0)
我得到了解决方案:
BaseObjectsrealmRepository:
改变这个:
旧: 抽象类BaseObjectsRealmRepository:BaseRepository {
新: 抽象类BaseObjectsRealmRepository构造函数(clazz:Class):BaseRepository {
现在是下一个代码:
override fun findAll(clazz: Class<T>): List<T> {
val allResults = findAllRealm(clazz)
return allResults
}
private fun findAllRealm(clazz: Class<T>): List<T>{
val realm = Realm.getDefaultInstance()
val realmResults = realm
.where(clazz)
.findAll()
val allResults: List<T> = realm.copyFromRealm(realmResults)
return allResults
}
谢谢!