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

Add findOrCreate helper methods #6168

Merged
merged 2 commits into from
Sep 24, 2018
Merged

Conversation

cmelchior
Copy link
Contributor

This mirrors API methods found in the Swift and Objective-C API's.

In their case it exists on the RealmList<Permission> object, but

  1. That is an extension method on the RealmList<Permission> only, which is impossible to achieve in Java unless we subclass RealmList. This is achievable and we need to do it to support the ACL property better anyway, but....

  2. It is also much harder to do and is one extra level of indirection on RealmPermissions and ClassPermissions so having it directly on those classes will be nicer anyway.

}

// Find existing permission object or create new one
Permission permission = permissions.where().equalTo("role.name", roleName).findFirst();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if there are already multiple permission with the provided role?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it is a List sync will remove duplicates. Ideally, this should have been a Set, but we are still lacking Core/Sync support for it.

// Find existing role or create new one
Role role = realm.where(Role.class).equalTo("name", roleName).findFirst();
if (role == null) {
role = realm.createObject(Role.class, roleName);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user still needs to find the role in order to add members to it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but you can do that through the permission object returned using findOrCreate("new-role").getRole().addMember("foo")

@cmelchior cmelchior merged commit f9b665a into master Sep 24, 2018
@cmelchior cmelchior deleted the cm/permissions-helper-methods branch September 24, 2018 10:26
@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