我尝试将Iterable添加到Realm,但它不起作用。
public List<Country> add (List<Country> countries){
db.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(countries);
}
});
return countries;
}
当我使用将域添加到单个对象时,它有效吗?
public Country add (Country country){
db.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealm(country);
}
});
return country;
}
这是我的模型我尝试了许多带有主键的变体,但它不起作用:(
public class City extends RealmObject {
@Required
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我重写了我的程序,并且添加到Realm根本不起作用。 这就是我初始化的方式。
public class AppGeo extends Application {
private static AppGeo instance;
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder()
.name("geoapp.realm")
.schemaVersion(1)
.deleteRealmIfMigrationNeeded()
.modules(new RealmModule())
.build();
Realm.setDefaultConfiguration(config);
instance = this;
}
public static AppGeo getInstance(){
return instance;
}
}
我打电话的方法
@Override
public void primaryInit(onGetAllCountryCallback callback) {
final Realm realmConnection = Realm.getDefaultInstance();
if(realmConnection.isEmpty()){
WebService webService = WebService.getInstace(AppGeo.getInstance());
Observable.defer(() -> Observable.just(webService.getCountriesInformation()))
.map(Parser::countriesParser)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<Country>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(List<Country> countries) {
callback.onSuccess(countries);
realmConnection.executeTransaction(realm -> realmConnection.copyToRealmOrUpdate(countries));
}
});
}
else {
findAll(callback);
}
realmConnection.close();
}