question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How get list of inner POJOs more simple?

See original GitHub issue

Realm 4.1.

My POJOs:

public class Store extends RealmObject {
    @PrimaryKey
    private int id;
    private RealmList<Product> products = new RealmList<>();
}

public class Product extends RealmObject {
    @PrimaryKey
    private int id;
    private String name;
}

I want to get list of all products of all stores.

Here my code.

public static List<Product> getProductsList() {
        Realm realm = Realm.getDefaultInstance();
        try {
            List<Product> allProductsList = new ArrayList<>();
            RealmResults<Store> realmResultsStores = realm.where(Store.class).findAll();
            for (Store store : realmResultsStores) {
                RealmList<Product> merachantProductsList = store.getProducts();
                allProductsList.addAll(realm.copyFromRealm(merachantProductsList));
            }
            return allProductsList;
        } finally {
            realm.close();
        }
}

It’s work. Fine. But I think it’s to much code. The question is: Is there an easier way to get list of all products of all stores?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Zhuindencommented, Oct 25, 2017

I recommend

public static RealmResults<Product> getProductsList(Realm realm) {
    return realm.where(Product.class).findAll();
}
0reactions
alexei-28commented, Oct 25, 2017

Yes, in my fragment I use

private RealmResults<Product> productRealmResults;

I use this to can add listener to Realm. And I use RealmRecyclerViewAdapter

So I return to fragment exact RealmResults<Product>.

OK. Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stream of a nested POJOs List to a another POJO LIST
You will first need a flatMap to create a stream of all InvoiceDetails in ... You can easily make a List from a...
Read more >
07 - Create Complex Nested Post Request body Using POJO ...
Here is the link of Full Play List ▻ https://bit.ly/2BY0vHuHere you will learn how to create a Complicated Post Request body on Runtime ......
Read more >
How to convert Nested JSON object to the Java ... - YouTube
In this video you will learn how to convert nested json object to the java pojo in easy way.Click here for More ......
Read more >
Nested pojo java class - What could I make better
Consider using constructors which set all fields (Lombok's @AllArgsConstructor ) which can make building the classes easier instead if ...
Read more >
Mapping Nested Values with Jackson - Baeldung
Learn three ways to deserialize nested JSON values in Java using the Jackson library.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found