Java – How do you find the ParseRoles currently associated with the current ParseUser?

How do you find the ParseRoles currently associated with the current ParseUser?… here is a solution to the problem.

How do you find the ParseRoles currently associated with the current ParseUser?

I want to populate the ListView with an Array or an ArrayList of a different ParseRole and the current ParseUser is associated with them. I think this requires some kind of query on the current user, but I’m not quite sure how to return the roles and how to return them to an Array or ArrayList that I can use to populate the ListView. I know you can use this to get the current user :

ParseUser.getCurrentUser();

But I can’t seem to find out from there how you set up the roles assigned to users.

Edit:

I’ve tried this now too:

ParseQuery<ParseRole> query = ParseRole.getQuery();
query.whereEqualTo("users", ParseUser.getCurrentUser().getObjectId());
query.findInBackground(new FindCallback<ParseRole>() {
   @Override
   public void done(List<ParseRole> objects, ParseException e) {
      if(e == null) {
         Toast.makeText(getApplicationContext(), objects.size() + "", Toast.LENGTH_SHORT).show();
      } else {
         Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();
      }
   }                      
});

But I still can’t seem to get any work related to this. I’ve tried everything I can think of so far, but haven’t really made any progress because the size of the “object” is still always 0.

Thanks in advance!

Answer:

After implementing the logic behind Marius Falkenberg Waldal’s answer and porting it to Android, I finally found a solution that suited my situation. I decided to post it in case it helps anyone in the future :

    ParseQuery<ParseRole> roleQuery = ParseRole.getQuery();
    List<ParseRole> allRoles = null;
    try {
        allRoles = roleQuery.find();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    userRoles = new ArrayList<ParseRole>();
    userRolesNames = new ArrayList<String>();
    for(ParseRole role : allRoles) {
        ParseQuery usersQuery = role.getRelation("users").getQuery();
        usersQuery.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
        try {
            if(usersQuery.count() > 0) {
                userRoles.add(role);
                userRolesNames.add(role.getName().toString());
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }               

GroupAdapter adapter = new GroupAdapter(this, userRolesNames);
    workspaces.setAdapter(adapter);

Solution

I stumbled upon this while trying to confirm something and realized there was an easier way :

Android

ParseQuery<ParseRole> query = ParseRole.getQuery();
query.whereEqualTo('users', ParseUser.getCurrentUser());
query.findInBackground(new FindCallback<ParseRole>() {
    public void done(List<ParseRole> roles, ParseException e) {
         do your thing with the roles list
    }
});

For the sake of others, answers in several other languages are provided here.

iOS Target-C

PFQuery *query = [PFRole query];
[query whereKey:@"users" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *roles, NSError *error) {
  if (!error) {
     Do something with the found roles
  }
}];

iOS swift

var query = PFRole.query();
query.whereKey('users', equalTo:PFUser.currentUser());
query.findObjectsInBackgroundWithBlock {
  (roles: [PFObject]!, error:NSError!) -> Void in
  if error == nil {
     do your thing with the roles
  }
}

JavaScript

var query = new Parse.Query(Parse.Role);
query.equalTo('users', Parse.User.current());
query.find().then(function(roles) {
   do your thing with the roles array
});

Related Problems and Solutions