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. – Riki Rikmen 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 – Riki Rikmen Jun 8 '16 at 15:37

1 Answer

up vote 0 down vote accepted

Do this:

           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<PersonInMenu> 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 appropriate override for createObject() which actually takes a primary key as its second parameter.

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

answered Jun 8 '16 at 20:04
EpicPandaForce

42k13115216
  •  
    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) – Riki Rikmen 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 – Riki Rikmen 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 – EpicPandaForce Jun 11 '16 at 12:40
  • 1
    thankyou EpicPandaForce, you are Realm Master haha.. marked it as answer – Riki Rikmen Jun 11 '16 at 13:08
    https://stackoverflow.com/questions/37705565/realm-update-failed-android

Realm Update failed - Android的更多相关文章

  1. Weblogic AdminServer启动失败,<Security> <BEA-090870> <The realm "myrealm" failed to be loaded

    服务器重装,环境配置正常,domain没有变动,启动AdminServer失败. AdminServer_log: <2016-9-29 上午09时43分12秒 GMT+08:00> &l ...

  2. Proxmox VE中出现TASK ERROR: command 'apt-get update' failed: exit code 100的解决方法

    问题描述: 出现这个错误一般在WEB或者在Proxmox VE的服务器上面能看到日志: PVE中出现TASK ERROR: command 'apt-get update' failed: exit ...

  3. Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK - Android

    在Windows7上运行 “cordova build android” 报错,如下: C:\test\hello> cordova build android ANDROID_HOME=C:\ ...

  4. glide install失败 Update failed for golang.org/x/net: Cannot detect VCS

    失败信息: [WARN]    Unable to checkout golang.org/x/net[ERROR]    Update failed for golang.org/x/net: Ca ...

  5. CocoaPods 升级1.8.4的坑 CDN: trunk Repo update failed

    之前升级了cocoaPods 版本1.8.4,今天pod install,然后问题就来了: 1.出现了下边的问题: Adding spec repo `trunk` with CDN `https:/ ...

  6. CDN: trunk Repo update failed - CocoaPods

    解决方案: 1.podfile文件中添加source源:  source 'https://github.com/CocoaPods/Specs.git' 2.执行 pod repo remove t ...

  7. 转: app端数据库(性能高) realm (ios, android 均支持)

    转:  http://ios.jobbole.com/85041/ 移动端数据库新王者:realm 2016/05/14 · iOS开发 · 数据库 分享到:0 原文出处: 没故事的卓同学(@没故事的 ...

  8. ubuntu apt update failed to fetch

    When I do command sudo apt update, always get belowing errors: Err:1 http://archive.ubuntu.com/ubunt ...

  9. Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK. Looked here: C:\Users\Administrator\AppData\Local\Android\sdk\tools\templates\gradle\wrapper

    在Windows7上运行 “cordova build Android” 报错,如下: C:\test\hello> cordova build androidANDROID_HOME=C:\U ...

随机推荐

  1. html5——3D案例(音乐盒子、百度钱包)

    1.音乐盒子:触碰盒子,盖子会打开 2.百度钱包:触碰钱包,钱包,会180度旋转 <!DOCTYPE html> <html lang="en"> < ...

  2. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

  3. git连接github mac

    话不多说,终端里的代码直接复制过来 Last login: Fri May 17 21:45:31 on ttys000 liuduoduodeMacBook-Air:~ liuxiangyang$ ...

  4. python爬虫15 | 害羞,用多线程秒爬那些万恶的妹纸们,纸巾呢?

    有时候 只是在人群中多看了一眼 就再也没办法忘掉那些容颜 小帅b在普通的一天 上着普通的网 不小心打开了一个不太普通的网站 https://www.mzitu.com/ 从此进入了不普通的一天 看着不 ...

  5. C语言考试

    1.如何定义一个占用空间为0的变量 2.c++如何在指定内存区域创建对象 3.gcc动态库如何调用宿主执行文件的函数,阐述动态链接库的两种加载方式 4.static有什么用处 5.阐述do{...}w ...

  6. BUPT2017 springtraining(16) #6 ——图论

    题目链接 A.容易发现最后字符的对应都是一对一的 或者说我们没办法出现最后多对一或者一对多的情况 所以只要算出 ‘a’ - 'z' 每个字符最后对应的字符即可 #include <cstdio& ...

  7. noip模拟赛 拼不出的数

    分析:如果每个数可以选任意多次,那么就是一个很普通的dp问题,这里每个数只能选一次,还是考虑dp,设f(i)表示1~i是否都能选上.考虑下一个数j,如果j > i + 1,那么i+1这个数就选不 ...

  8. git游戏杂谈

    git作为一个知名小游戏,在被Linus开发出来后就广受好评,在程序员圈子内迅速传播,以至于现在很多程序员可以一日无饭,却不能一日无git.是什么能让各路程序员如此着迷?今天,让我们走进git,看一看 ...

  9. 51Nod——T 1113 矩阵快速幂

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 基准时间限制:3 秒 空间限制:131072 KB 分值: 40 ...

  10. N天学习一个linux命令之kill

    用途 用于终止进程 用法 kill [-s signal|-p] [--] pid... kill -l [signal] 说明 1.默认发送信号15(请求终止进程,程序可以捕获,操作系统会杀死没有对 ...