Kingdom: how to request many, many fields

I recently heard about Realm for android (also available for iOS) in this talk by Joaquim Verques . A very interesting and very powerful tool for persistent data.

I decided to try it after the video, research and read the documentation.

It seemed to me that it is very easy to use and configure, but I ended up stuck in the middle of my project because I was not able to successfully make a request with many, many relationships.

I created a small example for this section.

My models:

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;
        }
    }

So far so good, now I'm going to save the list of feeds in realm (make it permanent), and then try to make some kind of request.

    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();
                        }
                    }
    }

, : "java.lang.IllegalArgumentException: : ".

, : , , , 1 = "test"

+4
1

"", "". :

RealmResults<Feed> realmResults3 = realm.where(Feed.class).equalTo("terms.categories.name", "test").findAll();
+6

All Articles