Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I m learning Realm and Recyclerview. I made simple app where you can simple add items and delete them. Everything is working fine until when i start deleting item one by one. At the very last item when I click delete it give me 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 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)
like image 411
Samar Ali Qazalbash Avatar asked May 04 '16 13:05

Samar Ali Qazalbash


1 Answers

Ok 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());
        }
    }
like image 181
Samar Ali Qazalbash Avatar answered Nov 02 '22 07:11

Samar Ali Qazalbash