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. HDU3949 XOR(线性基第k小)

    Problem Description XOR is a kind of bit operator, we define that as follow: for two binary base num ...

  2. fcc 响应式框架Bootstrap 练习3

    class="container-fluid"控制宽度100% <div class="container-fluid"> <h3 class ...

  3. Android Thermal-engine

    Thermal Engine Thermal 相关的东西主要在Vendor/qcom/proprietary/thermal-engine 目录下: thermal-engine.conf 文件可以用 ...

  4. DeltaFish 小组成员及个人博客地址

    艾寅中  http://www.cnblogs.com/aiyz 陈志锴  http://www.cnblogs.com/chenzhikai 李   鑫  http://www.cnblogs.co ...

  5. vue iView 打包后 字体图标不显示

    问题描述: 今天webpack打包后发现iView 字体图标不显示 解决方案: build/webpack.prod.conf.js 这个文件里面 module: { rules: utils.sty ...

  6. 02--SQLite操作一步到位

    SQLite数据库(一):基本操作 SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使用方便.结构紧凑.高效.可靠. 与其他数据库管理系 ...

  7. [Windows Server 2003] 安装IIS6.0及FTP

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装IIS6. ...

  8. THREE.js代码备份——canvas - geometry - earth(球体贴纹理)

    <!DOCTYPE html> <html lang="en"> <head> <title>three.js canvas - g ...

  9. Twisted web开发教程

    最近在网上看到一篇twisted web开发文章,将它实践了一下,twisted 提供基本的url路由 和 控制器,模板与模型需要外部扩展 1.目录浏览 2.get请求 3.url路由 4.接受带参数 ...

  10. time模块和datatime模块

    一.time模块 time.time() 获取时间戳 time.sleep() 睡几秒 time.gmtime() utc时间元组 time.localtime() 本地时间元组 time.mktim ...