Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

io.realm.exceptions.RealmException: ContactsModel is not part of the schema for this Realm

Getting above exception while trying to access Realm database.

Here is my code:

public Observable<RealmResults<ContactsModel>> getAllContacts() {
        RealmResults<ContactsModel> contactsModel = realm.where(ContactsModel.class).notEqualTo("id", PreferenceManager.getID(mContext)).equalTo("Exist", true).findAllSorted("Linked", Sort.DESCENDING, "username", Sort.ASCENDING).sort("Activate", Sort.DESCENDING);
        return Observable.just(contactsModel);
    }

I had tried some these solutions but didn't help me.

Disable instant run

Arrange apply plugins

MyApplication Class

    Realm.init(this);

    private static RealmConfiguration getRealmDatabaseConfiguration() {
            return new RealmConfiguration.Builder().name(getInstance().getString(R.string.app_name) + PreferenceManager.getToken(getInstance()) + ".realm").deleteRealmIfMigrationNeeded().build();
        }

        public static Realm getRealmDatabaseInstance() {
            return Realm.getInstance(getRealmDatabaseConfiguration());
        }

        public static boolean DeleteRealmDatabaseInstance() {
            return Realm.deleteRealm(getRealmDatabaseConfiguration());
        }

Build Gradle

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.sis.smartmessenger"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    testCompile 'junit:junit:4.12'

    //butter knife
    compile 'com.jakewharton:butterknife:8.6.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
    compile 'com.squareup.retrofit2:retrofit:2.2.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
    compile 'com.squareup.okhttp3:okhttp:3.6.0'
    compile 'com.squareup.retrofit2:converter-gson:2.2.0'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.0.7'
    compile 'de.greenrobot:eventbus:2.4.0'
    compile 'com.orhanobut:logger:1.15'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'jp.wasabeef:glide-transformations:2.0.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'jp.wasabeef:picasso-transformations:2.1.0'
    compile 'com.github.hani-momanii:SuperNova-Emoji:1.1'
    compile 'com.googlecode.libphonenumber:libphonenumber:8.3.2'
    compile('io.socket:socket.io-client:0.8.3') {
        // excluding org.json which is provided by Android
        exclude group: 'org.json', module: 'json'
    }


}

ContactModel Class

public class ContactsModel implements RealmModel {
    @PrimaryKey
    private int id;
    private int contactID;
    private String username;
    private String phone;
    private String phoneTmp;
    private boolean Linked;
    private boolean Activate;
    private boolean Exist;
    private String image;
    private String status;
    private String status_date;
    @Expose
    private String userState;

    public ContactsModel() {

    }
    public String getPhoneTmp() {
        return phoneTmp;
    }

    public void setPhoneTmp(String phoneTmp) {
        this.phoneTmp = phoneTmp;
    }
    public String getUserState() {
        return userState;
    }

    public void setUserState(String userState) {
        this.userState = userState;
    }


    public boolean isActivate() {
        return Activate;
    }

    public void setActivate(boolean activate) {
        Activate = activate;
    }

    public boolean isExist() {
        return Exist;
    }

    public void setExist(boolean exist) {
        Exist = exist;
    }


    public int getContactID() {
        return contactID;
    }

    public void setContactID(int contactID) {
        this.contactID = contactID;
    }


    public String getStatus_date() {
        return status_date;
    }

    public void setStatus_date(String status_date) {
        this.status_date = status_date;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public boolean isLinked() {
        return Linked;
    }

    public void setLinked(boolean linked) {
        Linked = linked;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
like image 791
Manish Avatar asked Mar 08 '23 13:03

Manish


1 Answers

I think you have problem with you Realm Configuration, and try by creating Model by extend RealmObject instead implement

Add defaultConfiguration

==> SetDefaultConfiguration <==

Realm.init(this);

RealmConfiguration mRealmConfiguration = new RealmConfiguration.Builder()
                .name("yourDBName.realm")
                .schemaVersion(1) // skip if you are not managing
                .deleteRealmIfMigrationNeeded()
                .build();

Realm.getInstance(mRealmConfiguration);
Realm.setDefaultConfiguration(mRealmConfiguration); // add this line in your configuration

Then get Realm as,

Realm.getDefaultInstance();
like image 77
Uttam Panchasara Avatar answered Apr 25 '23 12:04

Uttam Panchasara