【问题标题】:RealmList with Android带有 Android 的 RealmList
【发布时间】:2019-10-16 09:43:42
【问题描述】:

Items upon inserting

Items upon retrieving

事情是这样的:

我有一个扩展 RealmObject 的对象产品。 在 Product 对象内部,有一个属性:RealmList。 Bundle 还扩展了 RealmObject。

在Bundle Object里面,有一个属性:RealmList。 ProductBundle 扩展了 RealmObject。

现在所有这些列表在它们应该被插入时都不是空的,也不是空的。

插入后,如果我向 Realm 查询产品列表,我将收到一个包含 RealmList 列表的列表。但是,在每个 Bundle 项中,RealmList productBundles 始终为空。

class Product extends RealmObject{
   RealmList<Bundle> bundles= new RealmList<Bundle>();
   boolean isLocallyAddedToCart;
}

class Bundle extends RealmObject{
  RealmList<ProductBundle> productsBundles = new RealmList<ProductBundle>();
}

class ProductBundle extends RealmObject{
  String title;
}

对此有什么想法吗??

查询:

RealmResults<Product> cartItem = realm.where(Product.class).equalTo("isLocallyAddedToCart", true).findAll();

插入:

public void insertAddToCart(@NonNull ArrayList<Product> items) {
    try {
        Realm realm = getRealmInstance();
        realm.beginTransaction();
        realm.insertOrUpdate(items);
        realm.commitTransaction();
        realm.close();
    } catch (Exception ex) {
        ex.getStackTrace();
    }
}

处理对象:

RealmList<BundleProductOption> bundleProductOptions = new RealmList<>();
 private SparseArray<List<ProductBundle>> optionsFinalSelection = new SparseArray<>();
BundleProductOption bundleProduct = new BundleProductOption();                 

bundleProduct.setDefaultTitle(bundleProductOption.getDefaultTitle());

bundleProduct.setOptionId(bundleProductOption.getOptionId());  

bundleProduct.setParentId(bundleProductOption.getParentId());                          
bundleProduct.setRequired(bundleProductOption.getRequired());
bundleProduct.setType(bundleProductOption.getType());
RealmList<ProductBundle> productBundles = new RealmList<>();
productBundles.addAll(optionsFinalSelection.get(i));
bundleProduct.setProductsBundle(productBundles);
product.setSelectedOptions(bundleProductOptions);

【问题讨论】:

  • 在这里发布您的查询可能很有用,以及您在数据库中插入对象的代码
  • 查询看起来正确。您能分享一下您是如何插入产品的吗?
  • 添加了插入和检索查询。
  • 您是否测试过仅请求产品而不过滤 isLocallyAddedToCart。您可以调试请求以测试您是否真的在数据库中有对象
  • 在此处发布您是如何插入 Bundle 和 ProductBundle 对象的。也许没有由领域对象管理。

标签: android nested realm realm-list


【解决方案1】:

您必须在每个模型中使用 ID 作为主键来管理您的插入。 当你插入或更新时,你必须使用主键来保存数据的所有状态,然后你可以不使用它们,但领域需要它们来安排插入或更新方法。

让我了解您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多