java.lang.IllegalArgumentException:“值”不是有效的领域托管对象

时间:2019-02-20 13:06:16

标签: java android realm

从服务器读取数据后,我正在使用领域来存储数据,但是当我尝试将其存储在领域中时,我得到了“ java.lang.IllegalArgumentException:“值”不是有效的托管对象”

这是我在领域中存储数据的方法的代码

public void addOrdersToLocalDB(Order order,List<Product> products) {

    realmAsyncTask = myRealm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {

            Order localOrder = realm.createObject(Order.class, order.getId());
            localOrder.setName(order.getName());
            localOrder.setTimestamp(order.getTimestamp());
            localOrder.setDate(order.getDate());
            localOrder.setCost(order.getCost());
            localOrder.setProductNums(order.getProductNums());
            localOrder.setTime(order.getTime());

            Log.d("orders_data", "realm order : " + order.getName());

            RealmList<Product> localProducts = new RealmList<>();

            for (Product product : products){
                Log.d("orders_data", "realm product : " + product.getName());


                Product localProduct = realm.createObject(Product.class, product.getId());



                localProduct.setName(product.getName());
                localProduct.setBarCode(product.getBarCode());
                localProduct.setCurrentQuantity(product.getCurrentQuantity());
                localProduct.setStatus(product.getStatus());
                localProduct.setOldUnitPrice(product.getOldUnitPrice());
                localProduct.setImage(product.getImage());
                localProduct.setNeededQuantity(product.getNeededQuantity());
                localProduct.setTotalPrice(product.getTotalPrice());
                localProduct.setDescription(product.getDescription());
                localProduct.setUnitPrice(product.getUnitPrice());
                localProduct.setTimeStamp(product.getTimeStamp());

                localProducts.add(product);

            }

            if (localProducts.size() == products.size()){
                Log.d("orders_data", "realm products size : " + String.valueOf(localProducts.size()));
                localOrder.setProducts(localProducts);

            }



        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            HelperMethods.displayToastMsg("data added successfully to realm", HomeActivity.this);

            curvesLoader.setVisibility(View.GONE);

            //setupRecyclerView();


        }

    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {

            curvesLoader.setVisibility(View.GONE);
            HelperMethods.displayToastMsg("data not added to realm, there was a problem : " + error, HomeActivity.this);
            Log.d("myrealm", error.toString());

        }
    });
}

我搜索了该错误,并检查了这个问题here,但仍然收到该错误,希望有人可以提供帮助吗?

当我对静态数据使用其他方法(如下一个代码)时,它也可以正常工作

   Order order = realm.createObject(Order.class, "1");
            order.setName("طلب 1");
            order.setTimestamp("٢٠١٩.٠١.١٦.١١.٢٤.١٦");
            order.setDate("٢٠١٩.٠١.١٦");
            order.setCost("1050");
            order.setProductNums("2");
            order.setTime("١١.٣٧.٢٦");

            RealmList<Product> products = new RealmList<>();

            Double unitPrice = 10.0;
            int neededQuantity = 5;
            int currentQuantity = 0;
            Product product = realm.createObject(Product.class, "1");
            product.setName("مناديل فاين");
            product.setBarCode("6251001214468");
            product.setCurrentQuantity("0");
            product.setStatus(1);
            product.setOldUnitPrice(String.valueOf(unitPrice));
            product.setImage("https://www.albawaba.com/sites/default/files/im/pr_new/FINE_NEW_PACK.jpg");
            product.setNeededQuantity(String.valueOf(neededQuantity - currentQuantity));
            product.setTotalPrice(String.valueOf(unitPrice * neededQuantity));
            product.setDescription("الوزن : 100 منديل / العدد : 36 علبة");
            product.setUnitPrice(String.valueOf(unitPrice));
            product.setTimeStamp("٢٠١٩.٠١.١٦.١١.٣٧.٢٦");

            products.add(product);

            unitPrice = 7.5;
            neededQuantity = 10;
            currentQuantity = 0;
            Product product2 = realm.createObject(Product.class, "2");
            product2.setName("بيبسى 1 لتر");
            product2.setBarCode("6223001360766");
            product2.setStatus(1);
            product2.setOldUnitPrice(String.valueOf(unitPrice));
            product2.setCurrentQuantity("0");
            product2.setImage("https://grocety.com/media/catalog/product/cache/2/small_image/228x/9df78eab33525d08d6e5fb8d27136e95/1/2/120-370x310.jpg");
            product2.setNeededQuantity(String.valueOf(neededQuantity - currentQuantity));
            product2.setTotalPrice(String.valueOf(unitPrice * neededQuantity));
            product2.setUnitPrice(String.valueOf(unitPrice));
            product2.setDescription("الحجم :1 لتر / العدد : 30");
            product2.setCurrentQuantity(String.valueOf(currentQuantity));
            product2.setTimeStamp("٢٠١٩.٠١.١٦.١١.٢٤.١٦");

            products.add(product2);

            order.setProducts(products);

1 个答案:

答案 0 :(得分:1)

localProducts.add(product)替换为localProducts.add(localProduct),您初始化了localProduct,但忘记将其添加到列表中,而是添加了不受Realm管理的产品。