我正在尝试更新我的回收器视图,以便在单击其中一个项目上的加号按钮时,该项目的数量文本视图将增加1.
以下是适配器中单击侦听器的代码。
holder.mQuantityPlusTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mEventBus.post(new CartQuantityPlusClickEvent(imgUrl));
notifyDataSetChanged();
}
});
以下是订阅EventBus的活动的代码。
@Subscribe(threadMode = ThreadMode.POSTING)
public void cartQuantityPlusClick(CartQuantityPlusClickEvent event) {
mClothingRepository.plusClothingCartQuantity(event.getImgUrl());
}
以下是实际数量更新的代码。
@Override
public void plusClothingCartQuantity(String imgUrl) {
CartItem item = mRealm.where(CartItem.class).equalTo("imgUrl", imgUrl).findFirst();
mRealm.beginTransaction();
if (item != null)
item.increasQuantity(); Log.d(TAG, item.getQuantityInCart() + "");
mRealm.commitTransaction();
}
当我运行应用程序并按下按钮时。尽管与减少有效数量的代码完全相同,但数量没有发生变化。
以下是减少数量的代码(适配器和事件总线的代码几乎相同)。
@Override
public void minusClothingCartQuantity(String imgUrl) {
CartItem item = mRealm.where(CartItem.class).equalTo("imgUrl", imgUrl).findFirst();
mRealm.beginTransaction();
if (item != null) {
if (item.getQuantityInCart() == DEFAULT_CART_ITEM_QUANTITY) {
item.deleteFromRealm();
} else
item.decreaseQuantity();
}
RealmResults<CartItem> cartItems = mRealm.where(CartItem.class).findAll();
if (cartItems.size() == 0)
mEventBus.post(new EmptyCartEvent());
mRealm.commitTransaction();
}
用于递增数量的代码中的日志语句表示item
变量为空且表示:
12-29 12:25:31.881 21538-21538/com.geteven.gooddeal E/REALM_JNI: jni: ThrowingException 8, The Realm is already in a write transaction in /Users/cm/Realm/realm-java-release/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 126, .
12-29 12:25:31.882 21538-21538/com.geteven.gooddeal E/REALM_JNI: Exception has been thrown: The Realm is already in a write transaction in /Users/cm/Realm/realm-java-release/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 126
12-29 12:25:31.884 21538-21538/com.geteven.gooddeal E/EventBus: Could not dispatch event: class com.geteven.gooddeal.event.CartQuantityPlusClickEvent to subscribing class class com.geteven.gooddeal.view.CartActivity
java.lang.IllegalStateException: The Realm is already in a write transaction in /Users/cm/Realm/realm-java-release/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 126
at io.realm.internal.OsSharedRealm.nativeBeginTransaction(Native Method)
at io.realm.internal.OsSharedRealm.beginTransaction(OsSharedRealm.java:282)
at io.realm.BaseRealm.beginTransaction(BaseRealm.java:389)
at io.realm.Realm.beginTransaction(Realm.java:135)
at com.geteven.gooddeal.repository.ClothingRepository.plusClothingCartQuantity(ClothingRepository.java:145)
at com.geteven.gooddeal.view.CartActivity.cartQuantityPlusClick(CartActivity.java:190)
at java.lang.reflect.Method.invoke(Native Method)
at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:485)
at org.greenrobot.eventbus.EventBus.postToSubscription(EventBus.java:416)
at org.greenrobot.eventbus.EventBus.postSingleEventForEventType(EventBus.java:397)
at org.greenrobot.eventbus.EventBus.postSingleEvent(EventBus.java:370)
at org.greenrobot.eventbus.EventBus.post(EventBus.java:251)
at com.geteven.gooddeal.adapter.CartAdapter$3.onClick(CartAdapter.java:122)
at android.view.View.performClick(View.java:6289)
at android.view.View$PerformClick.run(View.java:24800)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
我从存储库类的构造函数中获取我的realm实例,其中包含所有业务逻辑。
public ClothingRepository() {
mEventBus = EventBus.getDefault();
mRealm = Realm.getDefaultInstance();
mClothingRef = FirebaseDatabase.getInstance().getReference()
.child(FIREBASE_CLOTHING_REFERENCE);
}
我不关闭它,因为我不知道该怎么做。我觉得这可能是问题的一部分。我该如何解决这个问题?
答案 0 :(得分:0)
在使用之前,你应该始终获得领域的实例。并在使用后关闭实例。
@Override
public void plusClothingCartQuantity(String imgUrl) {
Realm realm = Realm.getDefaultInstance();
CartItem item = realm.where(CartItem.class).equalTo("imgUrl", imgUrl).findFirst();
realm.beginTransaction();
if (item != null)
item.increasQuantity(); Log.d(TAG, item.getQuantityInCart() + "");
realm.commitTransaction();
realm.close()
}
请参阅我应该使用的功能代码。首先,每次需要任何数据库操作时,都不应使用mRealm字段并调用Realm.getDefaultInstance()
。
使用后,请不要忘记致电realm.close()