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
Related
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 am using Realm for android 1.2.0 and when querying for multiple ids:
query.in(Constants.ID, arraysOfIdsString);
The results are not returned in the order given in the array. Is this a realm bug? Can i preserve somehow the order of the ids with the order of the elements returned?
You can specify a sort order when doing the query:
query.int(Constants.ID, arrayOfIdString).findAllSorted(Constants.ID, Sort.DESC);
But if your list of Id's in the array is different than what is possible through sorting them either ASC or DESC, then that is unfortunately not possible.
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
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.
I need to check if two cursors pointing at the same rows with the same values. Is it possible?
More details:
I'm loading data from my own ContentProvider
I'm sending request to server and then updating my data inside ContentProvider with new values.
If values is changed - I need to notify user that he can update data.
As per CommonsWare's deleted answer:
Iterate over the relevant columns in the Cursor, retrieve the values, and compare each.
Although you may not know in advance the type of each column, you can find out with Cursor.getType(). You can also use Cursor.getColumnNames() to get the name of each column, and the number of columns.
This information will allow you to then use the correct accessor method to obtain each value and compare.
In SQLite, rows do not have an identity separate from their column values (but the ROWID is one of these values).
What you want requires that your data has some unique column(s) as part of the cursor, either the ROWID, or some other key value that is guaranteed to have no duplicates.
Otherwise, you can never know if what you see is just two records that happen to have the same values in those columns.