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. 在ubuntu系统下下载和卸载skype

    1.下载 sudo apt-get install skypeforlinux 2.卸载 sudo apt remove skypeforlinux

  2. java攻城狮之路--复习JDBC(PrepareStatement)

    PreparedStatement: 1.可以通过调用 Connection 对象的 preparedStatement() 方法获取 PreparedStatement 对象 2.PreparedS ...

  3. VC使用CryptoAPI计算MD5

    // md5.h #include <tchar.h> #include <wincrypt.h> // 计算Hash,成功返回0,失败返回GetLastError() // ...

  4. RTL Compiler之synthesis flow

    1 generic RTL Compiler work flow 2 invoking RTL compiler RTL Compiler is invoked from the operating ...

  5. UIResponder详解

    UIResponder Class Reference Managing the Responder Chain 1.- (UIResponder *)nextResponder 返回接收者的下一个相 ...

  6. scp: /xxxx: not a regular file

    问题描述 scp root@10.2.1.92:/home /home/wangju/databakroot@10.2.1.92's password: xxxxscp: /home: not a r ...

  7. windows 小知识---windows下生成公钥和私钥

    首先Windows操作系统需要安装git. 安装完成后,再到任意的文件夹内,点击右键.选择git bash here 打开之后,输入ssh-keygen,一路按enter键. 全部结束后,再到C:\U ...

  8. map集合遍历,放入id

    背景,需要从电脑导入excel表格到网页上然后表格中公司需要对应数据库的id 通过key-value方法来对应id Office office = new Office();office.setG00 ...

  9. C++项目作业 学生管理系统

    /*Student.h*/#pragma once #include<string.h> using namespace std; #include<string> class ...

  10. 洛谷 1486 郁闷的出纳员【Treap】

    [题意概述] 要求维护一个序列支持以下操作: 1,插入元素x: 2,把序列的所有元素加上x: 3,把序列的所有元素减去x,同时低于一个给定的下限的元素马上被删除: 4,询问序列中第k大的元素. [题解 ...