gpt4 book ai didi

java.lang.IllegalArgumentException : 'value' is not a valid managed object with realm 异常

转载 作者:行者123 更新时间:2023-11-29 18:34:37 31 4
gpt4 key购买 nike

我在从服务器读取数据后使用 Realm 来存储数据,但是当我尝试将其存储在 Realm 中时,我得到了“java.lang.IllegalArgumentException: 'value' is not a valid managed object”

这是我在 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());

}
});
}

我搜索了那个错误并检查了这个问题 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);

最佳答案

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

关于java.lang.IllegalArgumentException : 'value' is not a valid managed object with realm 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54787084/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com