question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query latest models distinct by update_time

See original GitHub issue

Goal

I’m converting a sqlite database to Realm and have a model that has fields are groupID and updated_time. I want to select all latest model in each group.

Expected Results

I expect to get the list that contains all latest model sorted by updated_time DESC.

Actual Results

RealmResult.distinct() is deprecated, RealmQuery.distinct() cannot make sure result is latest.

Steps & Code to Reproduce

I don’t have data right now @@

Code Sample


RealmResults<MyModel> realmResults = getRealm().where(MyModel.class)
					.notEqualTo(MyModel.GROUP_ID, 0)
					.notEqualTo(MyModel.IS_DELETED, 0)
					.findAllSorted(MyModel.LAST_UPDATE, Sort.DESCENDING);
			realmResults.distinct(MyModel.GROUP_ID); <<< Deprecated

Version of Realm and tooling

Realm version(s): 3.3.1

Realm sync feature enabled: no

Android Studio version: 2.3.3

Which Android version and device: Galaxy Note 5 Android 6.0.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Zhuindencommented, Jun 12, 2017

Tricky question because core needs to be released.

Current core release is 2.8.3, and this would probably be in 2.9.0, which will need to show up in Realm-Java…

1reaction
Zhuindencommented, Jun 12, 2017

This core PR got merged 3 hours ago which allows this use case to work, see https://github.com/realm/realm-core/pull/2644

This means it’ll need to be included in object store and then exposed in Java binding (probably just removing depreciation will be enough though)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django: filter by latest per distinct column - Stack Overflow
This translates to a SELECT DISTINCT ON SQL query. Here's the difference. For a normal distinct() call, the database compares each field in...
Read more >
The SQL SELECT DISTINCT Statement - Laracasts
Hi I want to run a query just give me value with no duplicate. What would be the query in laravel 5 using...
Read more >
Selecting distinct values from dupes based on latest date
I have a table of patients that has often multiple occurrences of a patient number but with differing dates of which I need...
Read more >
SELECT DISTINCT FROM <model > (DMX) - SQL Server
Returns all possible states for the selected column in the model. The values that are returned vary depending on whether the specified ...
Read more >
db.collection.distinct() — MongoDB Manual
A query that specifies the documents from which to retrieve the distinct values. ... When specifying collation, the locale field is mandatory; all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found