Realm Update failed - Android
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
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.
- 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 aRealmList<T>
, added modified code – EpicPandaForce Jun 11 '16 at 12:40 - 1thankyou EpicPandaForce, you are Realm Master haha.. marked it as answer – Riki Rikmen Jun 11 '16 at 13:08https://stackoverflow.com/questions/37705565/realm-update-failed-android
Realm Update failed - Android的更多相关文章
- 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 ...
- Proxmox VE中出现TASK ERROR: command 'apt-get update' failed: exit code 100的解决方法
问题描述: 出现这个错误一般在WEB或者在Proxmox VE的服务器上面能看到日志: PVE中出现TASK ERROR: command 'apt-get update' failed: exit ...
- 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:\ ...
- 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 ...
- CocoaPods 升级1.8.4的坑 CDN: trunk Repo update failed
之前升级了cocoaPods 版本1.8.4,今天pod install,然后问题就来了: 1.出现了下边的问题: Adding spec repo `trunk` with CDN `https:/ ...
- CDN: trunk Repo update failed - CocoaPods
解决方案: 1.podfile文件中添加source源: source 'https://github.com/CocoaPods/Specs.git' 2.执行 pod repo remove t ...
- 转: app端数据库(性能高) realm (ios, android 均支持)
转: http://ios.jobbole.com/85041/ 移动端数据库新王者:realm 2016/05/14 · iOS开发 · 数据库 分享到:0 原文出处: 没故事的卓同学(@没故事的 ...
- ubuntu apt update failed to fetch
When I do command sudo apt update, always get belowing errors: Err:1 http://archive.ubuntu.com/ubunt ...
- 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 ...
随机推荐
- P2639 [USACO09OCT]Bessie的体重问题Bessie's We…
题目描述 Bessie像她的诸多姊妹一样,因为从Farmer John的草地吃了太多美味的草而长出了太多的赘肉.所以FJ将她置于一个及其严格的节食计划之中.她每天不能吃多过H (5 <= H & ...
- Angular——事件指令
基本介绍 angular的事件指令都是ng-click,ng-blur....的形式,类似于js的事件 基本使用 <!DOCTYPE html> <html lang="e ...
- Json——一般应用
引用命名空间 using Newtonsoft.Json; 序列化类或者类的集合 string jsonData1 = JsonConvert.SerializeObject(p1);//序列化类 s ...
- unable to get system library for the project
当向eclipse导入项目实例后,项目上出现红叉的错误提示,在项目属性里的Java Build Path里发现了错误提示复选选项: unable to get system library for t ...
- js >> 右移操作符
十进制 十六进制 二进制 右移>> 十进制值 F 1F FF
- Eclipse中自动生成get/set时携带注释给get/set
Eclipse中自动生成get/set时携带注释给get/set 编码的时候通常要用到 JavaBean ,而在我们经常把注释写在字段上面,但生成的Get/Set方法不会生成,通过修改Eclips ...
- GridView中字符串太长处理方式
<asp:TemplateField HeaderText="子机构编号"> <ItemTemplate> <asp:Label ID="L ...
- pandas.DataFrame.quantile
pandas.DataFrame.quantile 用于返回数据中的 处于1/5 1/2(中位数)等数据
- vue中的父组件及子组件生命周期的执行顺序
一.没有任何任何显示与隐藏限制条件的情况下: 1.运行的顺序依次是: 父组件created→父组件beforeMounted→子组件created→子组件beforeMounted→子组件mounte ...
- jQuery匹配id 批量修改css属性
<ul id="foreignCurrencyTree_1_ul"> <li id="foreignCurrencyTree_11_li"&g ...