ahmadalibaloch
ahmadalibaloch

Reputation: 6021

Failed to handle RealmList<RealmList<RealmInt>> in Realm.io

My JSON data looks like this from server api

{
  //...
  PredecessorIds:[[1,2][3,4][5]]
  //...
}

I can successfully handle the arrays of Integer or String by RealmList<RealmInt> but this time I failed with an error, because RealmList> is not supported saying, "Type parameter 'io.realm.realmList' is not within its bounds...."

For RealmInt see this link.

I tried to solve it using RealmList<RealmLista> where RealmLista extends from RealmObject and has a RealmList like this

public class RealmLista extends RealmObject {
public RealmList<RealmInt> value;
public RealmLista() {
}

public RealmLista(RealmList<RealmInt> val) {
  this.value = val;
}

}

and then created a RealmListaTypeAdapter and added it to Gson but when deserializing Gson expects an Object (RealmLista) but is found array, as the data shown above from server is obvious.

//RealmListAdapter for Gson
@Override
public RealmLista read(JsonReader in) throws IOException {
    RealmLista lista = new RealmLista();
    Gson gson = new Gson();
    //how to read that [[1],[3,4]] int into RealmLista
    in.beginArray();
    while (in.hasNext()) {
        lista.value.add(new RealmInt(in.nextInt()));
    }
    in.endArray();
    return lista;
}

Is there any way to store a simple List<List<Integer>> by converting to RealmObject of any type while saving, List<List<Integer>> is easily converted by Gson. :-/

Upvotes: 1

Views: 302

Answers (1)

Christian Melchior
Christian Melchior

Reputation: 20126

Realm doesn't support lists of lists currently. See https://github.com/realm/realm-java/issues/2549.

So @EpicPandaForce's idea about creating a RealmObject that holds that inner list is probably the best work-around.

It could look something like this:

public class Top extends RealmObject {
  private RealmList<ChildList> list;
}

public class ChildList extends RealmObject {
  private RealmList<RealmInt> list;
}

public class RealmInt extends RealmObject {
  private int i;
}

The correct link for the gist should be: https://gist.github.com/cmelchior/1a97377df0c49cd4fca9

Upvotes: 1

Related Questions