0

When I am filtering a list in an adapter where I fetched the list from the Realm Database I am getting error. This is my ItemFilter class.

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        String query = constraint.toString().toLowerCase();

        FilterResults results = new FilterResults();
        final List<friend_model> list = original_items;
        final List<friend_model> result_list = new ArrayList<>(list.size());

        for (int i = 0; i < list.size(); i++) {
            String str_title = list.get(i).getName();
            if (str_title.toLowerCase().contains(query)) {
                result_list.add(list.get(i));
            }
        }

        results.values = result_list;
        results.count = result_list.size();

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filtered_items = (List<friend_model>) results.values;
        notifyDataSetChanged();
    }
}

And I am getting the error message like

An exception occured during performFiltering()!
                                                    java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
                                                        at io.realm.Realm.checkIfValid(Realm.java:191)
                                                        at io.realm.RealmResults.get(RealmResults.java:111)
                                                        at io.realm.RealmResults.get(RealmResults.java:55)
                                                        at com.mathi.finapp.adapters.Friend_adapter$ItemFilter.performFiltering(Friend_adapter.java:150)
                                                        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                        at android.os.Looper.loop(Looper.java:136)
                                                        at android.os.HandlerThread.run(HandlerThread.java:61)
Mathi Vanan
  • 101
  • 1
  • 9
  • That exception is pretty obvious and clear; you cannot read realm objects in one thread and expect to use them in a different thread; remember realm objects are not copied – Eenvincible Apr 13 '17 at 21:43
  • 1
    See doc https://realm.io/docs/java/latest/#threading – beeender Apr 14 '17 at 08:39

0 Answers0