Sol
Sol

Reputation: 843

Android realm findAllAsync() freezing UI

when I'm querying lot of elements the UI get freeze.

realm.where(TVRealm.class).equalTo("favorite", true).findAllAsync()
                .addChangeListener(new RealmChangeListener<RealmResults<TVRealm>>() {
                    @Override
                    public void onChange(RealmResults<TVRealm> element) {
                        List<TV> tvList = new ArrayList<>();
                        for (TVRealm tvRealm : element) {
                            tvList.add(prepareTV(TVRealm.toTV(tvRealm), true));
                        }
                        listener.onDataChange(tvList);
                    }
                });

I thought that findAllAsync() will run on other thread and avoid the issue but not. Does anyone knows how to avoid this issue? Maybe there is another way without using findAllAsync() method.

Thanks.

Upvotes: 2

Views: 3409

Answers (3)

S Kranthi Kumar
S Kranthi Kumar

Reputation: 760

why dont you try this

RealmResult<item> res=realm.where(item.class).where("name","sanjay").findAll();

it will get all the data at a time.

Upvotes: -1

savepopulation
savepopulation

Reputation: 11921

According to official Realm document, findAllAsync works in a background thread. https://realm.io/docs/java/latest/#asynchronous-queries

I think your data changes too often and so you're trying to notify ui too often. So you're blocking ui. I guess you are notifying a RecyclerView adapter in your onDataChange method.

Also if your result list has too many items, every time when your data changed exploring the results and adding items to a new list may block ui.

Upvotes: 2

Vladimir Parfenov
Vladimir Parfenov

Reputation: 665

If I don't make mistake, while RealmResults is updating, for each change you create new collection with new models and update UI. Try to call findAll() in another thread, map results to TVs and post completed list of TVs to UI thread.

Upvotes: 1

Related Questions