Skip to content
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

Querying the existence of data #6499

Closed
wants to merge 1 commit into from
Closed

Conversation

anggrayudi
Copy link

I am very often using this query to check if Realm contains specific data in a table. We could prevent code repetition using these two methods.

In many lines, I usually write the following code:

if (realm.where(Purchase.class).equalTo("sku", "pro").equalTo("purchased", true).exists()) {
    // activate paid items
}

@Zhuinden
Copy link
Contributor

Zhuinden commented Apr 24, 2019

Hmm, I'm not an official Realm member, but I think while exists() can sound nice (although also easily possible to make into an extension function if you use Kotlin), the noData() name isn't that great for the other case.

I'd just prefer !realmQuery.exists().

@anggrayudi
Copy link
Author

anggrayudi commented Apr 25, 2019

I forgot to mention that we need to optimize exists() performance by adding limit(1). So it will look like this:

public boolean exists() {
    return limit(1).count() > 0;
}

@cmelchior
Copy link
Contributor

Given that exists() is just a shorthand for .count() > 0 or, as you point out .limit(1).count() > 0, then I'm not sure it is worth adding it to the API surface. It is just 1 extra method sure, but we also need to factor in the API surface of RealmQuery which is already rather large.

@cmelchior cmelchior closed this Jun 20, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants