zoukankan      html  css  js  c++  java
  • Realm Update failed

    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

    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.

    •  
      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
  • 相关阅读:
    First Missing Positive
    Find Minimum in Rotated Sorted Array II
    switch两种写法对比
    常用的前端JavaScript方法封装
    如何保证缓存和数据库的一致性?
    14个前端小知识
    dataTable转换特定的类
    C# MD5 32大写位加密 UTF-8编码
    另一个 SqlParameterCollection 中已包含 SqlParameter
    C#实现数据回滚,A事件和B事件同时执行,其中任何一个事件执行失败,都会返回失败
  • 原文地址:https://www.cnblogs.com/pengmn/p/9303707.html
Copyright © 2011-2022 走看看