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

Cancel Async query

Open cchuang6 opened this issue 9 years ago • 6 comments

Goal

I have a problem to cancel an async query. My async query takes a little bit of time and I want to cancel it before I go into another Fragment. Otherwise, the database will be locked down until the original query has been finished. I tried to use removeChangeListener but it still lock down my other query.

Expected Results

Use removeChangeListener to release the database resources.

Actual Results

I cannot do another query until the original query has been finished.

Steps & Code to Reproduce

Describe your current debugging efforts.

Code Sample

Aysnc Query


mUnOrganizedContacts = getRealm().where(Contact.class)
                        .isEmpty("groupList.contacts")
                        .findAllAsync();

mUnOrganizedContacts.addChangeListener(new RealmChangeListener<RealmResults<Contact>>() {
                    @Override
                    public void onChange(RealmResults<Contact> contacts) {
                        num.setText("(" + String.valueOf(contacts.size()) + ")");
                    }
                });

Remove Change listener

if(mUnOrganizedContacts != null)
            mUnOrganizedContacts.removeAllChangeListeners();

Version of Realm and tooling

Realm version(s): ? 3.5.0 Realm sync feature enabled: yes/no

Android Studio version: ?

Which Android version and device: ?

cchuang6 avatar Jul 26 '17 22:07 cchuang6

what do you mean by "the database will be locked down until the original query has been finished."?

beeender avatar Jul 27 '17 02:07 beeender

Hi there @cchuang6 ! Can you give us more information on this issue? We are, in particular, confused by the term "locked down". The Realm DB supports multiple concurrent modifications... -blake

bmeike avatar Jul 28 '17 16:07 bmeike

Well modifications no, there can be only one write transaction across processes, but there should be no read lock during query execution either.

How much data do you have in terms of row count, 100000?

Zhuinden avatar Jul 28 '17 17:07 Zhuinden

Totally my mistake. There can only be one write transaction across threads, of course.

The point I was trying to make is that queries should not, in any way, lock anything down. In fact, until you start trying to get values out of them they should be free...

bmeike avatar Jul 29 '17 01:07 bmeike

@bmeike they are not 100% free because they need to become isLoaded() which takes time, but it still shouldn't block anything either.

Zhuinden avatar Jul 29 '17 08:07 Zhuinden

Closing due to inactivity. @cchuang6 feel free to reopen if you have more information about this issue.

cmelchior avatar Aug 16 '17 09:08 cmelchior