-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LIMIT #544
Comments
Hi Greg Thank you for the feedback. RealmResults are lazy loaded, so they don't take up additional memory whether you have 100 or 1.000.000 results. Also the design intent was for RealmLists to behave like standard arrays, which means that subList() is the right way to do it. However you are not the first to ask for a limit functionality, so either we have to be better about communicating the right use pattern or perhaps add limit as a shorthand for subList() like you suggested. |
To clarify a little. While the results are lazy loaded in Android our Realm core does maintain a list of the results. However it is done in such a way that it is really lightweight and compact. |
Thanks. Using subList works fine for me in most circumstances, the particular use case where I think it would be useful to be able to specify a limit in the query is when using an autoupdating RealBaseAdapter. Perhaps this wasn't the intended use for the RealmBaseAdapter, but being able to specify a robust query for results (including sort order, as mention in my other enhancement request) that autoupdates would be very powerful. |
+1. Although the results are lazily loaded, List<Equipment> filtered;
try {
filtered = results.subList((page - 1) * 100, page * 100);
} catch (IndexOutOfBoundsException lastPage) {
try {
filtered = results.subList((page - 1) * 100, results.size());
} catch (IllegalArgumentException noMoreData) {
filtered = new ArrayList<>();
}
} |
@haskellcamargo Isn't it the dev's job to check the size before calling subList? I maintain a utility method |
Not exactly in this case. In case of |
You would also have safety using
That said we are not against adding some kind of LIMIT functionality as it can also have a positive impact on performance alongside the other benefits you mentioned. It does not have a high priority however, so I cannot give you any timeline for when we can add it. |
👍 |
3 similar comments
👍 |
👍 |
👍 |
+1 |
3 similar comments
👍 |
👍 |
👍 |
@carloseduardosx I don't understand why you guys want a LIMIT. All you need to do is SORT your RealmResults, and then do not index above your arbitrary threshold. For example, in a Results that "contains" 225 elements, to obtain the second page (1 -> 2) of 50, all you need to do is index like this
Adapter:
Then index
I did not test this but conceptually this is equivalent to a "LIMIT" as a query in terms of functionality in your application. There is no point to limiting a Results. Just paginate your elements. You guys don't even need |
Although I guess I can see a |
@Zhuinden And we don't understand why you guys dont implement the
|
@ahmadalibaloch hey, I'm just a community member, not an official realm person 👅 But to paginate the results, you just have to limit your index to show from the lowest and highest bound of your index, and show item size of the minimum of 30 or the remaining element count I'd assume the reason why this is not trivial is because of |
Another use case is to delete first n objects from a query results. |
Core issue tracking this: realm/realm-core#1239 |
I have a Record Table. I want to display 5 records every time, user clicks on Next button until last record of Table. Please help me.. Thank you for your time... |
@dtmandroid it is actually very simple, you could do something in your adapter: public class MyAdapter {
private int baseIndex;
public void selectPage(int page) {
baseIndex = 5 * page;
}
@Override
public void getIndex(int pos) {
return adapterData.get(baseIndex + pos);
}
@Override
public void getCount() {
return adapterData.size() - 5 > 0 ? 5 : adapterData.size() - baseIndex;
}
} The above just shows the basic idea how to achieve you use case, I am sure you need to write more code to make a full implementation. |
@beeender Thank for the answer. I could get first 5 records but crashes when get next 5 records. So will you please explain where to write selectPage() and getIndex() method and use of it.. Thank You, |
@dtmandroid you can also try this approach http://stackoverflow.com/a/38873498/2413303 |
Please please please implement this. Calling |
I also have problem with Here is my code realm.where(SomeClass::class.java)
.findAll()
.asFlowable()
.map { it.count() } I use |
You might want to try |
It would be very useful to be able to specify the maximum number of objects to return in a query, similar to LIMIT in SQL. Otherwise the RealmResults list must be manually trimmed with subList.
The text was updated successfully, but these errors were encountered: