How I can select first N element from Realm database.
Now,I use this code to select N elements:
Realm.getDefaultInstance()
.where(MyObject.class)
.findAllSorted("myField", Sort.DESCENDING)
But this select operation is too long. I need to SQL 'LIMIT' operation analog.
You can have a look at this
https://github.com/realm/realm-java/issues/544
seems realm results are lazy loaded so you dun need "limit" when using realm
Simple
int N=10; // whatever value you want
Realm mRealm=Realm.getDefaultInstance();
RealmResults<Example> list= mRealm.where(Example.class).findAll();
list.subList(0,N);
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.
`
Possible duplicate Since I already posted the solution here : Limit Realm results
I figured out the solution to achieve this after so many days using "between" query as found in the official docs https://realm.io/docs/java/latest
If you want to fetch first N elements, Simply pass the count from and to with field name as below
realm.where(clazz).between("count",0,1000).findAll();
Where,
"count" = Field name (i.e variable name in your pojo)
"0" = Range From
"1000" = Range to
For example: The above query will fetch first 0 to 1000 as RealmResults.
Note: The above solution works only if you have some unique id with row count. In my case I manually inserted count value before inserting the values into Realm.
Related
How can I change the order or index of Realm object in the android studio??
I want to change the order of the objects, I cleared the class and tried to insert a new one, Is that the solution. Please help me
Instead of using findAll() method you can use findAllSorted() and you can also give ASCENDING or DESCENDING order.
From Realm documentation:
findAllSorted(java.lang.String[] fieldNames, Sort[] sortOrders)
Finds all objects that fulfill the query conditions and sorted by
specific field names.
Parameters:
fieldNames - an array of field names to sort by.
sortOrders - how to sort the field names.
Returns: a RealmResults containing objects. If no objects match the
condition, a list with zero objects is returned.
I just inserted a column sort_order and updated that each time.
Works perfectly. Thanks
In realm we have the option to find the first element in the database using findFirst(). But I want to do the reverse of it. I want to last element based on the some condition which I provide.
To find out the first element in the realm database:
GamesDetail learnGameDetail = realm.where(GamesDetail.class).equalTo("gameType", getResources().getString(R.string.learn_row)).notEqualTo("currentBadge",0).findFirst();
In the above query all the rows will have currentBadge as zero. But the last updated row will have currentBadge with some value. I need to find the last updated row with currentBadge not equal to zero.
Instead of
GamesDetail learnGameDetail = realm.where(GamesDetail.class)
.equalTo("gameType", getResources().getString(R.string.learn_row))
.notEqualTo("currentBadge",0)
.findFirst();
You can do
GamesDetail learnGameDetail = realm.where(GamesDetail.class)
.equalTo("gameType", getResources().getString(R.string.learn_row))
.notEqualTo("currentBadge",0)
.findAll()
.last(null);
Please note that insertion order is not kept after deletion. Using findAllSorted() is generally advised.
I'm changing my application from SQLite to Realm and I have next question.
I have next SQLite statement and I want to convert into Realm:
SELECT * from Table1 WHERE Table1.column1 = ?
AND Table1.column2 = ?
AND Table1.column3 < Table1.column4;
While that sort of query is supported by the underlying storage engine, that capability is not yet exposed in the Java API. You can follow https://github.com/realm/realm-java/issues/1615 for progress on that.
What is the use of findFirst() method in Realm for Android? Also, how to retrieve the last inserted record from a table using Realm?
findFirst() would be used when you only expect one result from your query, or just don't care about the whole list, and only want to sample one record.
If you want the very last element you could try findAll(), get the size of that list, and get the last element.
I've a column/field called last_message_time of type Date in my Table A.
Suppose querying Table A returns x results.
How do i sort these results based on dates inside last_message_time column.
Example, in SQLite we have ORDER BY date(dateColumn)
RealmResults<A> sorted = realm.where(A.class)
.findAllSorted("last_message_time", Sort.ASCENDING);
EDIT: since Realm 4.3.0, the following is preferred:
RealmResults<A> sorted = realm.where(A.class)
.sort("last_message_time", Sort.ASCENDING)
.findAll();
Use just "sort"!
"findAllSorted" is deprecated!
io.realm.RealmQuery.findAllSorted(String)
Since 4.3.0, now use RealmQuery.sort(String) then RealmQuery.findAll() Finds all objects that fulfill the query conditions and sorted by specific field name in ascending order.
Sorting is currently limited to character sets in 'Latin Basic', 'Latin Supplement', 'Latin Extended A', 'Latin Extended B' (UTF-8 range 0-591). For other character sets, sorting will have no effect.
More details in: LINK