Java.lang.IllegalStateException: The object is no longer managed by Realm. Has it been deleted?

I am studying Realm and Recyclerview. I made a simple application where you can just add items and delete them. Everything works fine until I start deleting the item one by one. In the very last paragraph, when I click delete, it gives me an exception

java.lang.IllegalStateException: Object is no longer managed by Realm. Has it been deleted? 

This is my code.

  @Override public void onDeleteClick(int position) { mRealm.beginTransaction(); mResult.get(position).deleteFromRealm(); mRealm.commitTransaction(); adapter.notifyItemRemoved(position); } 

Here is the full stack

  java.lang.IllegalStateException: Object is no longer managed by Realm. Has it been deleted? at io.realm.internal.InvalidRow.getStubException(InvalidRow.java:192) at io.realm.internal.InvalidRow.getString(InvalidRow.java:88) at io.realm.PersonsRealmProxy.realmGet$firstName(PersonsRealmProxy.java:67) at techiespk.realmpractice.Persons.getFirstName(Persons.java:22) at techiespk.realmpractice.MyListAdapter.onBindViewHolder(MyListAdapter.java:44) at techiespk.realmpractice.MyListAdapter.onBindViewHolder(MyListAdapter.java:15) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283) 
+7
android android-recyclerview realm
source share
1 answer

Well thanks to Christian Melchior for help. I fixed my code by doing this

Before

  @Override public void onBindViewHolder(MyListViewHolder holder, int position) { Persons p = persons.get(position); holder.firstName.setText(p.getFirstName()); holder.lastName.setText(p.getLastName()); } 

After

  @Override public void onBindViewHolder(MyListViewHolder holder, int position) { if (persons.get(position).isValid()) { Persons p = persons.get(position); holder.firstName.setText(p.getFirstName()); holder.lastName.setText(p.getLastName()); } } 
+7
source share

All Articles