境界:充满RealmList的RealmObject大小为零(Realm: The size of

2019-10-28 08:36发布

场景:
我有一个模型(DBBasket)在本地继续存在,产品附加值和产品本身的数量。
当用户的每个产品的缩略图下点击+按钮

  • 我越来越totalQuantityProduct
  • 添加产品到DBBasket
  • 设置totalProducts这是我从服务器获取,在DBBasket了。

代码中的+按钮:

holder.HomeProductBindGrid.thatPlusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Product productIns = mProductsList.get(holder.getAdapterPosition());
            mRealm = mRealmManager.getLocalInstance();
            mRealm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(@NonNull Realm realm) {
                    mProductRealm = realm.where(Product.class).equalTo(ProductFields.UNIQUE_ID, productIns.getUniqueId()).findFirst();
....


realm = mRealmManager.getLocalInstance();
realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        String totalNumberOfItemsInBasket = parseDataFromServer(ServerResponse, "numberOfProducts");
        if (totalNumberOfItemsInBasket.matches("")) {
            totalNumberOfItemsInBasket = "0";
        }

        Product product = realm.where(Product.class).equalTo(ProductFields.UNIQUE_ID, prod.getUniqueId()).findFirst();
        if (product == null) {
            product = realm.createObject(Product.class, mProductRealm.getUniqueId());
        }

        if (product != null) {
            if (product.totalQuantity.get() == null) {
                product.totalQuantity.set(0);
                product.totalQuantity.increment(countOrder);
            } else {
                product.totalQuantity.increment(countOrder);
            }
            realm.insertOrUpdate(product);

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

            dbBasket.getProducts().add(product);
            dbBasket.setTotalProducts(totalNumberOfItemsInBasket);
            realm.insertOrUpdate(dbBasket);
            Log.wtf("productRealm", dbBasket.getProducts().get(0).getUniqueId() + "");// It shows the UID correctly.
        }
    }
});

由于某些原因,因为,我清洁Product上的活动破坏,但不DBBasket

Realm realm = mRealmManager.getLocalInstance();
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(@NonNull Realm realm) {
            realm.delete(Product.class);
        }
    });

每个用户来到主网页的时候,我收到从服务器产品的列表,并把它们插入到本地数据库:

for (int j = 0; j < receivedProductsFromServer.getLength(); j++) {
    final Product product = new Product(...);

    product.setUniqueId(Utils.UniqueIdMaker());

    realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(@NonNull Realm realm) {
          Product productRealm = realm.where(Product.class).equalTo(ProductFields.UNIQUE_ID, product.getUniqueId()).findFirst();
          if (productRealm == null) {
            realm.insert(product);
          }
    }
});

问题:
现在,对于这以前添加到这些产品DBBasket ,我想展示自己totalQuantity+的前面。
所以,我上面的代码中的代码更改:

for (int j = 0; j < receivedProductsFromServer.getLength(); j++) {
    final Product product = new Product(...);

    product.setUniqueId(Utils.UniqueIdMaker());

    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(@NonNull Realm realm) {
            Product productRealm = realm.where(Product.class).equalTo(ProductFields.UNIQUE_ID, product.getUniqueId()).findFirst();
            if (productRealm == null) {
                DBBasket dbBasketRealm = realm.where(DBBasket.class).findFirst();

                if(dbBasketRealm != null) {
                    RealmList<Product> productInBasket = dbBasketRealm.getProducts(); //Size() is zero!!!

                    RealmResults<Product> productFiltered = productInBasket.where().contains(ProductFields.UNIQUE_ID, product.getUniqueId()).findAll();
                    Product p = productFiltered.get(0);

                    if(p != null) {
                        product.totalQuantity.set(0);
                        product.totalQuantity.increment(Integer.valueOf(p.getQuantity()));
                    } else Log.wtf("productRealm", "productFiltered is Null.");
                }
                realm.insert(product);
            }
        }
    });

但没有工作!dbBasketRealm.getProducts()大小为零。


编辑:
+按键上调试模式:

dbBasketRealm.getProducts()上调试模式:


DBBasket模型:

public class DBBasket extends RealmObject{
    public String totalProducts;
    public RealmList<Product> products;

    public DBBasket() {}
}

产品型号:

public class Product extends RealmObject implements Observable {
    @PrimaryKey
    @Required
    private String uniqueId;
    public final MutableRealmInteger totalQuantity = MutableRealmInteger.valueOf(0);

    public Product() {}
}

Answer 1:

获取以前添加的产品在篮子里,从服务器。 然后再次将它们添加到本地数据库。 类似下面的代码片段OnCreate例如:

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


文章来源: Realm: The size of filled RealmList in RealmObject is zero