На самом деле я пишу шаблон репозитория с Kotlin для проекта Android Studio.У меня есть общий интерфейс с именем «BaseRepository».Этот интерфейс расширен от "BaseRealmRepository" и других.
Мне нужно получить все результаты из Realm.db в findAll (), но я не могу изменить эту унаследованную функцию, потому что она является общей в других репозиториях.
Когда я пытаюсь, яполучение следующей ошибки:
"fun findAllRealm (clazz: Class): список не выполнен: выводимый тип GenericRealmModelType <</em>> не является подтипом RealmModel" *
, но моя настоящая проблема в том, что параметр клаза класса , что мне нужно выполнить приведение или что-то еще для перехода к предложению "где" из 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
}
Модуль приложения 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'
Что я могу сделать?
Спасибо!