2017-06-01 1 views
-1

アイテムのリストを表示するためにRecyclerviewがあり、Realmデータベースからデータを取り出しArrayListに格納しています。しかし、特定の位置の特定のBoxItemを更新しようとすると、このようなエラーが表示されます。通常のJavaオブジェクトとしてRealmObjectを継承するJavaクラスを使用できません

FATAL EXCEPTION: main           Process: one.thebox.android, PID: 7236 
                   java.lang.IllegalStateException: Changing Realm data can only be done from inside a transaction. 
                    at io.realm.internal.Table.throwImmutable(Table.java:1138) 
                    at io.realm.internal.Table.checkImmutable(Table.java:924) 
                    at io.realm.internal.UncheckedRow.setLong(UncheckedRow.java:195) 
                    at io.realm.BoxItemRealmProxy.realmSet$quantity(BoxItemRealmProxy.java:590) 
                    at one.thebox.android.Models.BoxItem.setQuantity(BoxItem.java:347) 
                    at one.thebox.android.adapter.SearchDetailAdapter$SearchedItemViewHolder.updateQuantityInCart(SearchDetailAdapter.java:870) 
                    at one.thebox.android.adapter.SearchDetailAdapter$SearchedItemViewHolder.access$1900(SearchDetailAdapter.java:492) 
                    at one.thebox.android.adapter.SearchDetailAdapter$SearchedItemViewHolder$7.onClick(SearchDetailAdapter.java:786) 
                    at android.view.View.performClick(View.java:4780) 
                    at android.view.View$PerformClick.run(View.java:19866) 
                    at android.os.Handler.handleCallback(Handler.java:739) 
                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                    at android.os.Looper.loop(Looper.java:135) 
                    at android.app.ActivityThread.main(ActivityThread.java:5254) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at java.lang.reflect.Method.invoke(Method.java:372) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

ここではいくつかのコードを見つけてください - ArrayList boxes;

boxItem.setQuantity(quantity); 
CartHelper.updateQuantityInCart(boxItem, quantity); 
boxItems.get(position).setQuantity(quantity); 
notifyItemChanged(position);  

ここでそれがエラーを示している、配列リスト内のオブジェクトを変更することができませんでし

BoxItem -

public class BoxItem extends RealmObject implements Serializable { 
private int quantity; 
@PrimaryKey 
private String uuid; 

public String getUuid() { 
    return uuid; 
} 

public void setUuid(String uuid) { 
    this.uuid = uuid; 
} 
    public int getQuantity() { 
    return quantity; 
} 

public void setQuantity(int quantity) { 
    this.quantity = quantity; 
} 
} 

そしてCartHelper-は

データベース - レルムでの更新データクラスです
public static void updateQuantityInCart(final BoxItem boxItem, final int quantity) { 
    Realm realm = TheBox.getRealm(); 
    realm.beginTransaction(); 
    BoxItem boxItem1 = realm.where(BoxItem.class).equalTo("uuid", boxItem.getUuid()).findFirst(); 
    if (boxItem1 != null) { 
     boxItem1.setQuantity(quantity); 
    } 
    realm.commitTransaction(); 
} 

updateQuantityInCart()は、アダプタクラスのメソッドであり、私たちが起動したいときに呼び出されますrecyclerviewの任意の項目を日付にします。その特定のアイテムの数量を更新する場合のように

+0

:あなたは、バックグラウンドスレッドで行う別の最善の方法は、可能性がCartHelper.updateQuantityInCart(boxItem, quantity);

// Obtain a Realm instance Realm realm = Realm.getDefaultInstance(); realm.beginTransaction(); //... add or update objects here ... realm.commitTransaction(); 

のようなすべての更新の間でbeginTransaction()commitTransaction()を追加する必要がありますトランザクション。 Realm 3.1.4以降を使用している場合は、 'RealmRecyclerViewAdapter'が' notifyItemChanged() '呼び出しを処理します。 – EpicPandaForce

+0

@EpicPandaForceので、RealmRecyclerViewAdapter'と私の現在のレルムバージョンを試してみてください。3.1.3 – rahul

+0

@rahul boxItem/CartHelperとそのupdateQuantityInCartメソッドは明確ではありません。 – Stallion

答えて

0

トランザクション外でRealmオブジェクトを修正しようとしているようですが、これは許可されていません。よくはい、 `レルムデータを変更すると、内側からのみ行うことができます

realm.executeTransactionAsync 
+0

この関数は、この関数CartHelper.updateQuantityInCart(boxitem、quantity)でデータベースを更新し、この内部トランザクションを使用しています。 しかし、この2行 - boxItems.get(位置).setQuantity(量); notifyItemChanged(position); はArrayListの一部です - Boxitemを特定の位置に更新する際にエラーが発生しています。 – rahul

+1

一般に、 'RealmResults'には' ArrayList'の中にRealmProxiesを管理しないでください。 – EpicPandaForce

関連する問題