Android realm findAllAsync() freezing UI

realm query android
realm tutorial (kotlin android)
realm migration android
realm kotlin
realm background thread android

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.

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.

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  (Also, a tip: you should open your first Realm instance on the UI thread only after the first Activity has been created, just to be safe from context.getFilesDir() == null).

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.

Android app freezes with 100K objects dataset · Issue #3532 · realm , Goal I'm thinking of building an Android app using Realm. Over time, its Whenever a new Item is created, the UI freezes for 3 to 5 seconds. from https://​github.com/realm/realm-android-adapters and findAllAsync() ? about the findAllAsync() and findAll about the findAllAsync() and findAll() problem #3756. EspoirX But if you use synchronous transactions on the UI thread

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.

Add support for frozen objects · Issue #6590 · realm/realm-java , findAllAsync().asObservable() .map((result, changeSet) -> { result.isFrozen() == true; }) // It is still possible to navigate and query a frozen Realm just like a live  UI or not UI? Typically Realm is fast enough to read and write data on Android’s main thread. However, write transactions are blocking across threads so in order to prevent accidental Application Not Responding Errors (ANR’s) there are advise to perform all Realm write operations on a background thread (not Android’s main thread).

Create reactive mobile apps in a fraction of the time, Step 2: Apply the realm-android plugin near the top of the application level build.​gradle This lets you keep your UI constantly updated with the latest content with findAllAsync(); result.load() // be careful, this will block the current thread until it returns In those cases it can be beneficial to freeze a Realm, RealmResults,  Here we want to do the work on the io() thread and get the results on the UI thread, but this will throw the dreaded llegalStateException: Realm access from incorrect thread. exception. Before discussing solutions I would like to iterate how Realm and RxJava respectively solve the concurrent modifications problems: Realm

How to use Realm for Android like a champ, and how to tell if you're , getDefaultInstance().where(…) it's going to break sooner or later. ( Also, a tip: you should open your first Realm instance on the UI thread only  (Android’s UI thread is a Looper.) This means the Realm instance will be periodically updated to the latest version. This lets you keep your UI constantly updated with the latest content with almost no effort!

WebView, A WebView is useful when you need increased control over the UI and advanced configuration options that will findAllAsync() provides a more robust solution. apply plugin: 'realm-android' Complete a gradle sync and you now have Realm added as a dependency to your project! Realm requires an initial call since 2.0.0 before using it. You can do this in your Application class or in your first Activity's onCreate method.

Comments
  • what realm version are you using?
  • Your UI freezes because your mapping logic happens on the UI thread, and evaluated for every single element - not because Realm is slow. Realm wasn't designed to have every single element read in a loop; that's why by default, RealmResults is just a lazy-loaded "cursor" that gives you lazy-loaded proxies
  • so the solution is to revise your code in such a way that tvList.add(prepareTV(TVRealm.toTV(tvRealm), true)); won't be there, and just use TVRealm directly
  • The data is not changing often it's happening when I create the fragment. I have a drawer layout when I select a drawer option I change fragment. When the drawer start closing the app freezes for a moment then the fragment is loaded correctly.
  • while opening / closing drawer inflating a fragment and replace it can block ui for a moment. one of the simplest way to avoid it, you can just wait drawer to close and after it's closed you can replace your fragment.