博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Realm Update failed - Android
阅读量:6849 次
发布时间:2019-06-26

本文共 4478 字,大约阅读时间需要 14 分钟。

0

I'm using realm for my android apps, So I want to update my Bill object using the same Primary key, but ended with

FATAL EXCEPTION: main Process: com.example.rikirikmen.billsplit, PID: 22045 io.realm.exceptions.RealmPrimaryKeyConstraintException: Value already exists: 1

realm.executeTransaction(new Realm.Transaction() {                    @Override                    public void execute(Realm realm) {                        Bill updateBill = realm.where(Bill.class).equalTo("Bill_ID", bill).findFirst();                        DetailMenu menu = realm.createObject(DetailMenu.class);                        menu.setMenuID(MenuID);                        menu.setMenuName(String.valueOf(menuName.getText()));                        menu.setMenuPrice(Price);                        menu.setQuantity(Qty);                        for (int i = 0; i < adapter.getPersonMenuObjList().size(); i++) {                            PersonInMenu pim = realm.createObject(PersonInMenu.class);                            pim.setPersonID(adapter.getPersonMenuObjList().get(i).getPersonID());                            pim.setStatus(adapter.getPersonMenuObjList().get(i).isStatus());                            menu.personInMenus.add(pim);                        }                        updateBill.detailmenu.add(menu);                        realm.copyToRealmOrUpdate(updateBill);                    }                });
  •  
    what do you mean ? i need to createobject? the object already created in other activity before.. i just want to update the object. –  Jun 8 '16 at 15:07
  •  
    what do you mean inside transaction ? actually realm has create a new method realm.executetransaction for replacing realm.begintransaction and commit –  Jun 8 '16 at 15:37

1 Answer

0 accepted

:

realm.executeTransaction(new Realm.Transaction() {                @Override                public void execute(Realm realm) {                    Bill updateBill = realm.where(Bill.class).equalTo("Bill_ID", bill).findFirst();                    DetailMenu menu = new DetailMenu();                    RealmList
personInMenus = new RealmList<>(); //added line menu.personInMenus = personInMenus; //added line menu.setMenuID(MenuID); menu.setMenuName(String.valueOf(menuName.getText())); menu.setMenuPrice(Price); menu.setQuantity(Qty); for (int i = 0; i < adapter.getPersonMenuObjList().size(); i++) { PersonInMenu pim = new PersonInMenu(); pim.setPersonID(adapter.getPersonMenuObjList().get(i).getPersonID()); pim.setStatus(adapter.getPersonMenuObjList().get(i).isStatus()); menu.personInMenus.add(pim); } updateBill.detailmenu.add(menu); realm.copyToRealmOrUpdate(updateBill); } });

Although if you're hesitant about saving detached objects to the Realm through copyToRealmOrUpdate(), then use the .

If you use createObject(clazz, primaryKey) then you won't need copyToRealmOrUpdate() in this case.

  •  
    thankyou for your advice, actually i dont know its working or not.. i just got new error Process: com.example.rikirikmen.billsplit, PID: 31338 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean io.realm.RealmList.add(io.realm.RealmModel)' on a null object reference at com.example.rikirikmen.billsplit.DialogActivity$1$1.execute(DialogActivity.java:101) –  Jun 11 '16 at 11:10
  •  
    my object are filled, i dont know where is wrong. here is the log 06-11 18:02:56.785 31338-31338/com.example.rikirikmen.billsplit I/Object: 6 true 06-11 18:02:56.785 31338-31338/com.example.rikirikmen.billsplit I/Object: 7 true 06-11 18:02:56.785 31338-31338/com.example.rikirikmen.billsplit I/Object: 8 false 06-11 18:02:56.785 31338-31338/com.example.rikirikmen.billsplit I/Object: 9 false 06-11 18:02:56.785 31338-31338/com.example.rikirikmen.billsplit I/Object: 10 false –  Jun 11 '16 at 11:10
  •  
    The personInMenus is null by default and I forgot to set it to be a RealmList<T>, added modified code –  Jun 11 '16 at 12:40
  • 1
    thankyou EpicPandaForce, you are Realm Master haha.. marked it as answer –  Jun 11 '16 at 13:08
    https://stackoverflow.com/questions/37705565/realm-update-failed-android

转载于:https://www.cnblogs.com/pengmn/p/9303707.html

你可能感兴趣的文章
hiberante 二级缓存设置
查看>>
Sphinx 增量索引更新
查看>>
linux脚本^M: bad interpreter:解决方法
查看>>
javascript 图片上传缩略图预览
查看>>
Java Service Wrapper使用总结
查看>>
Java:concurrent包下面的Map接口框架图(ConcurrentMap接口、ConcurrentHashMap实现类)...
查看>>
c#简单自定义异常处理日志辅助类
查看>>
Hibernate 、 Axis2发布
查看>>
使用IPostBackEventHandler让JavaScript“调用”回传事件
查看>>
深入理解计算机系统(3.1)---走进汇编的世界
查看>>
Shell 利用 curl 模拟登陆
查看>>
DIV相关的操作总结
查看>>
常用DOS命令参数详解
查看>>
CSS3 动画一瞥
查看>>
ZBarReaderView屏幕旋转问题
查看>>
算法:基于 RingBuffer 的 Queue 实现《续》
查看>>
ylb:SQL 系统函数
查看>>
Online SVG to PNG/JPEG/TIFF conversion
查看>>
ubuntu系统自带的火狐(firefox)如何安装Adobe Flash
查看>>
SQL Server创建表超出行最大限制解决方法
查看>>