Skip to content Skip to sidebar Skip to footer

Realm: The Size Of Filled Realmlist In Realmobject Is Zero

Scenario: I have a model(DBBasket) to persist locally, the number of added products and products itself. When the user clicks on the + button under each product thumbnail I'm incr

Solution 1:

Get the previously added products in the basket, from the server. Then add them again to the local database. Something like below snippet code in OnCreate for example:

for (int i = 0; i < receivedFromServer.getLength(); i++) {

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(@NonNull Realm realm) {
        Product productRealm = realm.where(Product.class).equalTo(ProductFields.UNIQUE_ID, receivedFromServer[i].getUniqueId()).findFirst();

        if (productRealm != null) {
            productRealm.setTotalQuantity(quantity));

            DBBasket dbBasket = realm.where(DBBasket.class).findFirst();
            if (dbBasket == null) {
                dbBasket = realm.createObject(DBBasket.class);
            }

            RealmList<Product> productRealmList = dbBasket.getProducts();
            productRealmList.add(productRealm);
            dbBasket.products = productRealmList;
            dbBasket.setProducts(productRealmList);
            dbBasket.totalProductsSetZero();
            dbBasket.setTotalProducts(quantity));
            realm.insertOrUpdate(dbBasket);
        }
    }
});

Post a Comment for "Realm: The Size Of Filled Realmlist In Realmobject Is Zero"