Realm-java: When will realm support LIMIT query??

Created on 15 Feb 2016  路  3Comments  路  Source: realm/realm-java

I have history table and need to shrink it periodically.
Now I delete first 50000 objects one by one like the following.

RealmResults<Entry> results = realm.where(Entry.class).findAllSorted("created");
List<Entry> entriesToDelete = new ArrayList<Entry>();
for (int i = 0; i < results.size() && entriesToDelete.size() < 50000 ; i++) {
    entriesToDelete.add(results.get(i));
}
for (Entry entry: entriesToDelete)
    entry.removeFromRealm();

This is too slow. are there any alternative to do same things??
Or when will realm support limit clause??

https://github.com/realm/realm-java/issues/544
this was created last year but still no limit clause???
your development speed is too slow.

T-Duplicate

All 3 comments

Hi @wyvern610

With limited resources we always have to prioritize which tasks to work on, and LIMIT normally has clear work-arounds.

In your case you could make the following two step algorithm instead:

RealmResults<Entry> results = realm.where(Entry.class).findAllSorted("created");
Long max = results.get(Math.min(results.size() - 1, 50000)).getCreated();

realm.beginTransaction();
realm.where(Entry.class).lessThanOrEqual("created", max).findAllSorted("created").clear();
realm.commitTransaction();

I'll close this as an duplicate. You can follow progress on limit in #544

The initial code would have been much faster if he had just used deleteFromRealm(); on the object instead of adding them to an ArrayList first which needs to reinitialize its internal array many many times.

Now Limit is supporting

I did that using limit method, You should use latest version classpath "io.realm:realm-gradle-plugin:5.8.0"

RealmResults<YourPOJOClass> realmResults = mRealm.where(YourPOJOClass.class).sort("createdTime").limit(10).findAll();
//here this record will be sorted by ascending order using schema name "createdTime" 
//this will return the 10 rows only.

`

Was this page helpful?
0 / 5 - 0 ratings