Realm: Cómo consultar con campo muchos a muchos

Recientemente oí hablar de Realm para android (también disponible para iOS) en esta charla de Joaquim Verques . Herramienta muy interesante y muy potente para datos persistentes.

Decidí darle una oportunidad después del video, investigando y leyendo la documentación.

Me pareció muy fácil de usar y configurar, pero termino atrapado en el centro de mi proyecto, porque no podía manejar para hacer una consulta con muchas a muchas relaciones.

He creado un pequeño ejemplo para este tema.

Mis modelos:

public class Feed extends RealmObject { @PrimaryKey private int id; private String title; private String description; private String link; private Terms terms; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public Terms getTerms() { return terms; } public void setTerms(Terms terms) { this.terms = terms; } } public class Terms extends RealmObject { private String tag; private RealmList<Category> categories; public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } public RealmList<Category> getCategories() { return categories; } public void setCategories(RealmList<Category> categories) { this.categories = categories; } } public class Category extends RealmObject { @PrimaryKey private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 

Hasta ahora tan bueno, ahora voy a guardar una lista de feeds al reino (lo hacen persistente) y luego tratar de hacer alguna consulta.

  public static void test(final Context context,final List<Feed> feedList) { Realm realm = null; try { realm = Realm.getInstance(context); realm.beginTransaction(); realm.copyToRealmOrUpdate(feedList); realm.commitTransaction(); RealmResults<Feed> realmResults = realm.where(Feed.class).findAll();// return me all feeds RealmResults<Feed> realmResults1 = realm.where(Feed.class).equalTo("id", 1).findAll(); // return feed with id 1 RealmResults<Feed> realmResults2 = realm.where(Feed.class).equalTo("terms.tag", "tech").findAll(); // return feeds //with tag = "tech" RealmResults<Feed> realmResults3 = realm.where(Feed.class).equalTo("terms.category.name", "test").findAll(); //exception here } } catch (Exception e) { //Empty Log.d("Test","",e); } finally { if (realm != null) realm.close(); } } } 

Todo funciona bien hasta la última consulta y obtengo esta excepción: "java.lang.IllegalArgumentException: consulta no válida: la categoría no hace referencia a una clase".

Así que mi pregunta es ¿Cómo puedo hacer ese tipo de consulta con éxito, ya que me gustará reino para devolverme cada feed con términos que tiene al menos 1 categoría con nombre = "prueba"

Muchas gracias

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.