Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

java - "Object" is not part of the schema for this Realm

When I call the realm.where(MessageEventModel::class.java).findAll()

it throws excepiton: this is error

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)

this is my Application class

class MyApp : Application() {

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

this is my Realm Model

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

And here is where I'm trying to retrieve models

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 configuration:

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"
    }
}

I tried everything what I could found on stack: clean build, rebuild project, enable annotation processor, reinstaling apk, invalidate caches / restart

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue where in gradle file.The problem where just an ordering rules of applying plugins, thanks to the @EpicPandaForce's comment, the problem has been solved, I'm writing answer, for helping others, if they miss the commented answer from @EpicPandaForce

I changed the ordering of

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

to

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

That's all, now everything works fine


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...