1

My goal is to display the received data from the Internet in a RecyclerAdapter. But when I tried to do this, I get an exception. I tried to make this code

myService.getFromNet(name)
            .subscribeOn(Schedulers.io())
            .map(Cook::getPizza)
            .flatMap(cooks -> {
                Realm.getDefaultInstance().executeTransaction(realm -> {
                    realm.delete(PizzaViewDB.class);
                    //But here there is an error
                    realm.insert(cooks);
                });
                return io.reactivex.Observable.just(cooks);
            })
            .onErrorResumeNext(throwable -> {
                Realm realm = Realm.getDefaultInstance();
                RealmResults<PizzaViewDB> results = realm.where(PizzaViewDB.class).findAll();
                //And below is problem - reason: no instance(s) if type variable(s) E exist to that List<E> comforms to String
                return io.reactivex.Observable.just(realm.copyFromRealm(results));
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(re -> {
                recyclerView.setAdapter(new CookAdapter(cook.getPizza()));
            });

But I still have an exception. My base class where I take my RealmList here:

@SerializedName("pizza")
@Expose
private RealmList<PizzaViewDB> pizza;

public RealmList<PizzaViewDB> getPizza() {
    return pizza;
}

And my fiels from Realm:

public class PizzaViewDB extends RealmObject implements RealmModel {
@PrimaryKey
private String subject;
private String comment;
private String date;

public void setSubject(String subject) {
    this.subject = subject;
}

public void setDate(String date) {
    this.date = date;
}

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

public String getSubject() {
    return subject;
}

public String getDate() {
    return date;
}

public String getComment() {
    return comment;
}
NyanNyan
  • 51
  • 1
  • 9

1 Answers1

0

realm.copyFromRealm() was added in 0.87.0 (and insert() was added in 1.1.0), so you are using a version that is older than that. You most likely have this in your dependencies (in your module-level gradle file:

dependencies {
    compile 'io.realm:realm-android:0.82.2' 
    // up to 0.87.5, although you have something older than 0.87.0
}

Instead you should have the following, especially if this is a new project:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "io.realm:realm-gradle-plugin:4.3.1"
    }
}

And

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

If this is an old project with an old version of Realm, you might want to read through my version migration guide.


Btw, your code would have a ton of cryptic errors over time, so here is how you should fix it (use try-with-resources):

        .flatMap(cooks -> {
            try(Realm realm = Realm.getDefaultInstance()) {
                realm.executeTransaction(r -> {
                    r.delete(PizzaViewDB.class);
                    //But here there is an error
                    r.insert(cooks);
            });
            return io.reactivex.Observable.just(cooks);
        })
        .onErrorResumeNext(throwable -> {
            try(Realm realm = Realm.getDefaultInstance()) {
                // you *might* need realm.refresh() here but probably not
                RealmResults<PizzaViewDB> results = realm.where(PizzaViewDB.class).findAll();
                return io.reactivex.Observable.just(realm.copyFromRealm(results));
            }
        })
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428