【问题标题】:"Object" is not part of the schema for this Realm“对象”不是此领域架构的一部分
【发布时间】:2017-03-22 09:25:01
【问题描述】:

当我拨打realm.where(MessageEventModel::class.java).findAll()

它抛出异常: 这是错误

java.lang.IllegalArgumentException: MessageEventModel is not part of the schema for this Realm
                                                                          at io.realm.internal.modules.CompositeMediator.getMediator(CompositeMediator.java:172)
                                                                     at io.realm.internal.modules.CompositeMediator.getTableName(CompositeMediator.java:90)

这是我的应用程序类

class MyApp : Application() {

override fun onCreate() {
    super.onCreate()
    Realm.init(this)
    val realmConfiguration = RealmConfiguration.Builder()
            .deleteRealmIfMigrationNeeded()
            .name("my_db")
            .build()
    Realm.setDefaultConfiguration(realmConfiguration)
}
}

这是我的领域模型

class MessageEventModel : RealmObject{
  constructor()
  var message = ""
  constructor(message: String) : this(){
    this.message = message
  }
}

这是我试图检索模型的地方

class AwesomeChatFragment : Fragment() {

private val realm: Realm by lazy { Realm.getDefaultInstance() }
private var notifications: RealmResults<MessageEventModel>? = null

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater?.inflate(R.layout.activity_awesome_chat, container, false)
    notifications = realm.where(MessageEventModel::class.java).findAll()
    return view
}
}

gradle 配置:

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'


buildscript {
    ext.kotlin_version = '1.1.1'
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.android.tools.build:gradle:2.3.0'
        classpath 'com.google.gms:google-services:3.0.0'
        classpath "io.realm:realm-gradle-plugin:3.0.0"
    }
}

我尝试了所有可以在堆栈上找到的东西: 清理构建、重建项目、启用注释处理器、重新安装 apk、使缓存无效/重新启动

【问题讨论】:

  • 我前段时间遇到过这个问题(不同的领域版本)。看看这是否有帮助:github.com/realm/realm-java/issues/2491
  • 申请kotlin-androidkotlin-android-extensinos 之前 realm-android
  • 你是天才

标签: java android realm kotlin


【解决方案1】:

gradle文件中的问题。只是应用插件的排序规则的问题,感谢@EpicPandaForce的评论,问题已经解决,我正在写答案,以帮助其他人,如果他们错过了评论的答案来自@EpicPandaForce

我改变了顺序

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'

就是这样,现在一切正常

【讨论】:

  • 不,这也行不通。 Realm 已经在这个问题上浪费了大约 2 个小时,还没有解决方案!
猜你喜欢
  • 1970-01-01
  • 2016-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多