简体   繁体   English

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

[英]java.lang.IllegalArgumentException: 'value' is not a valid managed object with realm

I'm using realm to storing data after reading it from the server but when I try to store it in realm I got "java.lang.IllegalArgumentException: 'value' is not a valid managed object" 从服务器读取数据后,我正在使用领域来存储数据,但是当我尝试将其存储在领域中时,我收到了“ java.lang.IllegalArgumentException:'value'不是有效的托管对象”

here is my code of the method to storing data in realm 这是我在领域中存储数据的方法的代码

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

        }
    });
}

I searched about that error and checked this question here and still got the error hoping if any one can help? 我搜索了该错误,并在此处检查了此问题,仍然收到该错误,希望有人可以提供帮助吗?

also when I use other method with static data like the next code it works fine 当我对静态数据使用其他方法(如下一个代码)时,它也可以正常工作

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM