2
votes

I have the objectID of a particular user (who is not the current user) and would like to find his Display Name, which is a custom field that I created.

As per Parse's guide, this code will create a list of users that match some particular attributes.

 ParseQuery<ParseUser> query = ParseUser.getQuery();
 query.whereEqualTo("gender", "female");
 query.findInBackground(new FindCallback<ParseUser>() {
 public void done(List<ParseUser> objects, ParseException e) {
 if (e == null) {
    // The query was successful.
} else {
    // Something went wrong.
  }
 }
});

There's no reason for me to make a list, however, if only one user has that objectID. Any way for me to just get the display name of a particular user?

1
if you're sure about the result, you can just pick up the first item of that list. You'll eventually need to create a list, but all you need is the first item. - dumbfingers
a query will always return a list. in your case, if the list is empty, it means that no user matches your criteria, if there is one item, fine, if there are more, it means that there is more that one item that matches. you can consider taking only the first, or raising an error. - njzk2
@ss1271 and @njzk2 are both wrong, you can and should get just a single row if you only want a single row. There is a little confusion of your code sample being a query that would match multiple rows but the wording of your question talking about matching by objectId. - Timothy Walters
@TimothyWalters thx for pointing out. u're right. if the author has the ObjectId then there should be only 1 result or nothing. - dumbfingers

1 Answers

3
votes

What you want instead is getFirstInBackground(), e.g.:

ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getFirstInBackground(someUserId, new GetCallback<ParseUser>() {
    public void done(ParseUser user, ParseException e) {
        if (e == null) {
            // The query was successful.
            // check if we got a match
            if (user == null) {
                // no matching user!
            } else {
                // great, get the name etc
            }
        } else {
            // Something went wrong.
        }
    }
});