java - 如何从具有不同结果的领域数据库中查询java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30978857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:55:17  来源:igfitidea点击:

how to query from realm database with distinct results java

javaandroidrealmnosql

提问by Mucahit

I have got a Realmobject class, and storing lots of data in there, imagine that I have a String uid;field. I want to get uid names, but on same uid names just only one time, For example

我有一个Realm对象类,并在其中存储了大量数据,想象一下我有一个 String uid;字段。我想获得 uid 名称,但只有一次在相同的 uid 名称上,例如

uid

用户界面

AA

机管局

AA

机管局

BB

BB

CC

抄送

DD

DD

BB

BB

BB

BB

I want to get just AA,

我只想得到 AA,

BB,

BB,

CC,

抄送,

DD.

DD。

Only one time. I looked over realm documentation but couldn't find anything.

只有一次。我查看了领域文档,但找不到任何内容。

Thanks for answers.

感谢您的回答。

回答by Isaac Kingston

UPDATED :

更新 :

You can use distinct() to get distinct entries for an object class.

您可以使用 distinct() 获取对象类的不同条目。

// Returns the set of users that all have a different name
RealmResults<User> users = realm.where(User.class).distinct("name");

Note: .distinct will only work on fields that are indexed (@Index or @PrimaryKey). It doesn't work with child object property.

注意: .distinct 仅适用于已编入索引的字段(@Index 或 @PrimaryKey)。它不适用于子对象属性。

You can find more information about this method here in the official documentation. https://realm.io/docs/java/latest/api/io/realm/Realm.html#distinct-java.lang.Class-java.lang.String-][1]

您可以在官方文档中找到有关此方法的更多信息。 https://realm.io/docs/java/latest/api/io/realm/Realm.html#distinct-java.lang.Class-java.lang.String-][1]

回答by Amol Suryawanshi

Please use below steps to work distinct on Realm

请使用以下步骤在 Realm 上进行不同的工作

Update your realm version to realm :1.2.0 . because in older version distinct not working properly.

将您的领域版本更新为 realm :1.2.0 。因为在旧版本中不同的不能正常工作。

add @Index property to variable on which you want to apply distinct

将 @Index 属性添加到要对其应用不同的变量

execute your query like this

像这样执行您的查询

RealmResult<Example> realmReault =realm.where(Example.class).distinct("uid");

to include realm dependency in your project you can add below line in build.gradle(Project)

要在您的项目中包含领域依赖项,您可以在 build.gradle(Project) 中添加以下行

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.1'

        classpath "io.realm:realm-gradle-plugin:1.2.0"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

Above code is tested and working properly

上面的代码已经过测试并且可以正常工作