领域:RealmObject中填充的RealmList的大小为零

时间:2018-02-28 12:39:18

标签: android realm realm-list

情境:
我有一个模型(DBBasket)在本地坚持,增加产品和产品本身的数量 当用户点击每个产品缩略图

下的 + 按钮时
  • 我在totalQuantity
  • 中增加了Product
  • 将产品添加到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()尺寸为零。

修改
+ 调试模式下的按钮: + Button on Debug mode

调试模式下的

dbBasketRealm.getProducts()
dbBasketRealm.getProducts() on Debug mode

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

1 个答案:

答案 0 :(得分:0)

从服务器获取以前添加的产品。然后将它们再次添加到本地数据库。例如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);
        }
    }
});