I'm try to use Realm Database in My project
Here are my code snippets
In Application java code
public class CoreApplication extends Application {
private static CoreApplication sInstance;
public static final String F_PREFERENCE = "K_PREFERENCES";
public static Realm realm;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
LocaleManager.setLocale(this);
realm.init(this);
realm = Realm.getDefaultInstance();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.name("mydb.realm")
.schemaVersion(2)
.build();
Realm.setDefaultConfiguration(realmConfiguration);
}
@Override
public void onTerminate() {
Realm.getDefaultInstance().close();
super.onTerminate();
}
}
RealmObject java class
public class CashierTable extends RealmObject implements Serializable {
@Index
@PrimaryKey
private long id;
private String name = "";
private String password = "";
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
When I try to call this function when App has started.I have a exception
public static CashierTable getSingleCashierTable() {
CoreApplication.realm = Realm.getDefaultInstance();
AtomicReference<CashierTable> cashierTable=new AtomicReference<>();
CoreApplication.realm .executeTransaction(realm -> {
cashierTable.set(realm.where(CashierTable.class).findFirst());
});
return cashierTable.get();
}
Here is a my log result
java.lang.RuntimeException: Unable to create application com.unipay.posApp.CoreApplication: java.lang.IllegalStateException: Schema validation failed due to the following errors:
- Type 'CashierTable' appears more than once in the schema.
I cleared cash,but still have same issue.
Can anyone explain me what's wrong in my code?
You cannot have two RealmObject classes with the same class name twice unless you use Realm 5.0+ and use @RealmClass(name="...
to specify a different table name for at least one of your classes.
package io.package.first;
public class Dog extends RealmObject {
}
package io.package.second;
@RealmClass(name="SecondDog")
public class Dog extends RealmObject {
}