情境:
我有一个模型(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()
尺寸为零。
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() {}
}
答案 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);
}
}
});