Search code examples
androidrealmandroid-databindingandroid-database

Realm and Android DataBinding


I'm trying to use Realm in my app, but without success. The problem seems to be the fact that i use databinding and Realm in the same application.

The objectif, is let the user to use View to enter some data that i get with databinding, and insert into a Realm Database

I have the following error :

Character 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) 
    at io.realm.RealmSchema.getTable(RealmSchema.java:296)
    at io.realm.Realm.createObjectInternal(Realm.java:817)
    at io.realm.Realm.createObject(Realm.java:801)

Here my Character declaration

@RealmClass
public class Character extends RealmObject implements Serializable
private String classGame;
private String name;
private RealmList<Item> equipments;
private RealmList<CaracteristicClass> caracteristicClasses;
private RealmList<Caracteristic> parchemin;
private RealmList<Caracteristic> caracteristics;
private RealmList<Sort> spells;
private int nbCaracteristicPointAvailable;
private ObservableField<String> nbCaracteristicPointAvailableStringOb;

private int level;



private String levelString;

public Character() {

    initCharacter();
}


private void initCharacter(){
    nbCaracteristicPointAvailableStringOb = new ObservableField<>();
    caracteristics = new RealmList<>();
    parchemin = new RealmList<>();
    spells = new RealmList<>();
     /*
     * Init base caracteristic
     */
    Caracteristic caracteristic = new Caracteristic(TypeCaracteristic.VITALITE.toString(),0,0,11,0);
    caracteristics.add(caracteristic);
    caracteristic = new Caracteristic(TypeCaracteristic.SAGESSE.toString(),0,0,12,0);
    caracteristics.add(caracteristic);
    caracteristic = new Caracteristic(TypeCaracteristic.FORCE.toString(),0,0,13,0);
    caracteristics.add(caracteristic);
    caracteristic = new Caracteristic(TypeCaracteristic.INTELLIGENCE.toString(),0,0,14,0);
    caracteristics.add(caracteristic);
    caracteristic = new Caracteristic(TypeCaracteristic.CHANCE.toString(),0,0,15,0);
    caracteristics.add(caracteristic);
    caracteristic = new Caracteristic(TypeCaracteristic.AGILITE.toString(),0,0,16,0);
    caracteristics.add(caracteristic);
    caracteristic = new Caracteristic(TypeCaracteristic.LEVEL.toString(),1,200,-1,200);
    caracteristics.add(caracteristic);

    /*
     *  Parchemin caractéristique
     */

    caracteristic = new Caracteristic(TypeCaracteristic.VITALITE.toString(),0,100,11,100);
    parchemin.add(caracteristic);
    caracteristic = new Caracteristic(TypeCaracteristic.SAGESSE.toString(),0,100,12,100);
    parchemin.add(caracteristic);
    caracteristic = new Caracteristic(TypeCaracteristic.FORCE.toString(),0,100,13,100);
    parchemin.add(caracteristic);
    caracteristic = new Caracteristic(TypeCaracteristic.INTELLIGENCE.toString(),0,100,14,100);
    parchemin.add(caracteristic);
    caracteristic = new Caracteristic(TypeCaracteristic.CHANCE.toString(),0,100,15,100);
    parchemin.add(caracteristic);
    caracteristic = new Caracteristic(TypeCaracteristic.AGILITE.toString(),0,100,16,100);
    parchemin.add(caracteristic);


    caracteristicClasses = new RealmList<>();
    CaracteristicClass caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.VITALITE.toString(),1);
    caracteristicClasses.add(caracteristicClass);
    caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.SAGESSE.toString(),3);
    caracteristicClasses.add(caracteristicClass);
    caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.AGILITE.toString(),1);
    caracteristicClasses.add(caracteristicClass);
    caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.CHANCE.toString(),1);
    caracteristicClasses.add(caracteristicClass);
    caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.FORCE.toString(),1);
    caracteristicClasses.add(caracteristicClass);
    caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.INTELLIGENCE.toString(),1);
    caracteristicClasses.add(caracteristicClass);

}
public Character(final String name, final int level ){
    initCharacter();

}

public String getClassGame() {
    return classGame;
}
public void setClassGame(String classGame) {
    this.classGame = classGame;
}

public String getName(){
    return name;
}

public void setName(String name) {
    this.name = name;
}

public RealmList<Item> getEquipments() {
    return equipments;
}

public void setEquipments(RealmList<Item> equipments) {
    this.equipments = equipments;
}

public RealmList<Caracteristic> getParchemin() {
    return parchemin;
}

public void setParchemin(RealmList<Caracteristic> parchemin) {
    this.parchemin = parchemin;
}

public RealmList<Caracteristic> getCaracteristics() {
    return caracteristics;
}

public void setCaracteristics(RealmList<Caracteristic> caracteristics) {
    this.caracteristics = caracteristics;
}

public RealmList<Sort> getSpells() {
    return spells;
}

public void setSpells(RealmList<Sort> spells) {
    this.spells = spells;
}

public int getLevel() {
    return level;
}

public String getLevelString() {
    return levelString;
}

public ObservableField<String> getNbCaracteristicPointAvailableStringOb() {
    return nbCaracteristicPointAvailableStringOb;
}

public void setNbCaracteristicPointAvailableStringOb(String nbCaracteristicPointAvailableStringOb) {
    this.nbCaracteristicPointAvailableStringOb.set(nbCaracteristicPointAvailableStringOb);
    this.nbCaracteristicPointAvailableStringOb.notifyPropertyChanged(BR.character);
}

public void setLevelString(String levelString) {
    this.levelString = levelString;
    try{
        if(!this.levelString.equals("")){
            this.level = Integer.parseInt(this.levelString);
            if(level>0 && level<=Constants.MAX_LEVEL){
                nbCaracteristicPointAvailable = (level -1) * 5;
                this.setNbCaracteristicPointAvailableStringOb(String.valueOf(nbCaracteristicPointAvailable));
            }
        }
    }catch (NumberFormatException ex){
        Log.i("Character","emptyString or Invalid");
    }
}

Here my module build.gradle

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'com.jakewharton.butterknife'

android {
   compileSdkVersion 25
   buildToolsVersion "25.0.1"
   defaultConfig {
       applicationId "*******"
       minSdkVersion 15
       targetSdkVersion 25
       versionCode 1
       versionName "0.0.0"
       testInstrumentationRunner    "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
       release {
           minifyEnabled false
           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       }
   }
   dataBinding{
       enabled = true;
   }
}

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.0.1'
compile 'com.android.support:design:25.0.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'com.android.support:cardview-v7:25.0.1'
compile 'com.android.support:support-v4:25.0.1'
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.3'
compile 'com.github.amlcurran.showcaseview:library:5.0.0'
compile 'org.jsoup:jsoup:1.7.3'
compile 'com.android.support:design:25.0.1'
compile 'com.google.android.gms:play-services-appindexing:9.8.0'
compile 'com.jakewharton:butterknife:8.4.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
compile 'org.apache.commons:commons-collections4:4.1'
compile 'org.apache.commons:commons-lang3:3.4'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
testCompile 'junit:junit:4.12'
compile project(path: ':androidhttprequestlibrairy')

}

Here my project build.gradle

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.0-alpha1'
    classpath "io.realm:realm-gradle-plugin:2.2.1"
    classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
  }
  allprojects {
      repositories {
       jcenter()
     }
  }

  task clean(type: Delete) {
      delete rootProject.buildDir
  }

Here my activity that use Realm and DataBinding

final ActivityCreateBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_create);
    ButterKnife.bind(binding.getRoot());
    Realm.init(getApplicationContext());
    RealmConfiguration config = new RealmConfiguration.Builder().build();
    realm = Realm.getInstance(config);
    realm.beginTransaction();
    character = realm.createObject(Character.class);
    binding.setCharacter(character);

And some line of code below in a listener i use

  realm.commitTransaction();

I don't understant what is the problem ...

Sincerely,


Solution

  • From the documentation:

    Field types

    Realm supports the following field types: boolean, byte, short, int, long, float, double, String, Date and byte[]. The integer types byte, short, int, and long are all mapped to the same type (long actually) within Realm.

    Moreover, subclasses of RealmObject and RealmList<? extends RealmObject> are supported to model relationships.

    Well, you also have this:

     private ObservableField<String> nbCaracteristicPointAvailableStringOb;
    

    That's not a supported RealmModel field, so that won't work. You'd have to add @Ignore annotation.

    @RealmClass
    public class Character extends RealmObject implements Serializable
        //...
        @Ignore
        private ObservableField<String> nbCaracteristicPointAvailableStringOb;
    

    Also, you can test if this will work at all, but I think it'd be safer to use a static factory method instead of initializing all these lists and stuff in the default constructor that is also used when instantiating managed proxies.

    public static Character create() {
        Character character = new Character();
        character.initCharacter();
        return character;
    }
    

    (and on a completely unrelated sidenote, I don't know why this class is Serializable, why would you want to throw a RealmObject through an ObjectOutputStream...?)



    EDIT: You can also try forcing it to work by adding android-apt plugin.

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.3.0-alpha1'
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
            classpath "io.realm:realm-gradle-plugin:2.2.1"
        }
    }
    

    Then

    apply plugin: 'com.android.application'
    apply plugin: 'com.neenbedankt.android-apt'
    apply plugin: 'realm-android'
    
    //...
    
    compile 'com.jakewharton:butterknife:8.4.0'
    // ...
    apt 'com.jakewharton:butterknife-compiler:8.4.0'