realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Cancel ongoing async query

Open khltrifork opened this issue 8 years ago • 3 comments

Goal

The ability to cancel an ongoing query.

Expected Results

Adding a '.cancelQuery()' function to the RealmQuery class, that would cancel the underlying query and release the database resources.

Q1 created Q1 starts (async) Q2 created Q1 removeChangeListener Q1 cancelQuery Q2 starts (async) Q3 created Q2 removeChangeListener Q2 cancelQuery Q3 starts (async) Q3 finished

Perhaps the 'removeChangeListener' is not needed if cancelQuery is implemented, it could simply ignore the changeListener,

Actual Results

The queries that are created after the first query aren't started until the first query is finished, which makes it extremely slow when starting many queries in a row.

Currently (using .findAllAsync()):

Q1 created Q1 starts (async) Q2 created Q1 removeChangeListener Q3 created Q2 removeChangeListener Q1 finished Q2 starts (async) Q2 #finished Q3 starts (async) Q3 finished

Steps & Code to Reproduce

I have a search field that queries a table of items. I currently have roughly 6000 items in this table.

Whenever the user types in 1 character, i start a query (for live-search). If the user types fast, i start more and more queries, possibly before the first query has finished. Whenever a new query is started, i call '.removeAllChangeListeners()' from the RealmResults that i got from the last query i started. This works as expected, and i don't get any callbacks for these items, but it does not stop the underlying query, it simply ignores the result. This causes the performance issues.

Version of Realm and tooling

Realm version(s): 5.1.0

Realm sync feature enabled: No

Android Studio version: 3.1.2 Stable

Which Android version and device: Android 4.4.2, a Samsung Galaxy S2. This is simply the slowest device i have available that have a high enough minimum API for my application. The issue is much easier to notice and test on a really slow device.

I found a similar post here regarding the same feature/issue, but it was closed due to inactivity from the user: https://github.com/realm/realm-java/issues/5029

khltrifork avatar May 14 '18 14:05 khltrifork

consider debouncing input by about 300ms

Zhuinden avatar May 14 '18 16:05 Zhuinden

Thats more of a bandaid than a proper fix though. I'm fairly certain that my case is not the most extreme, as i still have a rather "small" data set compared to some other people.

It could potentially work for some devices, but not all.

On a Samsung Galaxy S2, a query takes about 2 seconds. If the user takes more than 300 ms to type each character, it would still start multiple queries that would be quite slow. But on a newer device, such as my own OnePlus 5, the queries takes 100-150 ms, and then this entire thing is not needed.

Moonbloom avatar May 16 '18 07:05 Moonbloom

Hi @Moonbloom Thanks for the detailed description. Unfortunately, there are currently no great solutions to fix this.

There are multiple reasons this is becoming a problem:

  1. The native query is not cancelable, just like SQLite queries. So once a query is running the thread running it is blocked.

  2. We only have one background worker thread for queries as having too many open background Realms is also draining system resources (most noticeably file descriptors).

One solution from our end could be to have threadpool with worker threads that were killed rather quickly and then have a reasonable cap on how many background threads could be created, but that would just move the needle for when it became a problem, e.g. if you canceled 5 queries quickly you might still deplete the thread pool.

Another option would be for you to create your own threadpool system where you controlled how many cancelled queries would be allowed, but because we currently don't offer any great way to move RealmResults across threads, this would probably be quite annoying to implement as you would have to copy all results into memory (https://github.com/realm/realm-java/issues/4059 and #1208 would both solve this). No matter what, implementing something like this wouldn't be trivial.

Right now I would seriously consider debouncing as this is a normal strategy to throttle access to a shared limited or expensive resource.

cmelchior avatar May 16 '18 08:05 cmelchior