How to get the latest record of each item?
Hi there,
I'd like to select all latest record of each item, which is possible with GreenDAO, but I couldn't find an equivalent for ObjectBox yet.
Pseudocode:
List<MyItem> latestByName = box.query()
.orderDesc(MyItem_.creationDate)
.groupBy(MyItem._name)
.build()
.find();
The following resources unfortunately didn't solve the issue:
- https://github.com/objectbox/objectbox-java/issues/198
- http://objectbox.io/objectbox-1-4-property-queries-entity-inheritance/
- https://github.com/objectbox/objectbox-java/issues/422#issuecomment-379745214
Does anyone know a solution? Thank you!
find() will return you all items then you can just select the last item in the list?
items = query.find()
items.get(items.size -1)
find() will return you all items then you can just select the last item in the list?
items = query.find() items.get(items.size -1)
Thanks for your reply, but that's not what I'm looking for. I need the latest item of each, not the latest item.
Something like SELECT ... FROM ... WHERE ... ORDER BY creationDate DESC GROUP BY itemId.
Any idea?
ObjectBox is not based on rows and columns and thus has no "group by". Doesn't make much sense for objects.
You can first for distinct property values, and then do a regular query for each of those values. Let us know if you need additional help on that.
Closing this issue due to inactivity. :zzz: Please re-open with more details or submit a new issue. -ut
@JimmyTheNerd did not you find any solution for this?
find() will return you all items then you can just select the last item in the list?
items = query.find() items.get(items.size -1)Thanks for your reply, but that's not what I'm looking for. I need the latest item of each, not the latest item.
Something like
SELECT ... FROM ... WHERE ... ORDER BY creationDate DESC GROUP BY itemId.Any idea?
I agree that find() is not good as it dumps all the records, but didn't find an off-the-shelf method. I end up with the code below (Kotlin)
box.query().build().find(box.count() - 1, 1).last()