2

Hi I was wondering how I can get a ParseUser object from a ParseObject. I need to do this because a ParseQuery returns a List. Here is my code and thank you for the help!

        // get the currentUser
        currentUser = ParseUser.getCurrentUser();
        List<ParseObject> friendsList = currentUser.getList("friendsList");

        // search for the person the user wants to add as a friend
        List<ParseObject> userResults = new ArrayList<ParseObject>();
        ParseQuery otherUserQuery = ParseUser.getQuery();
        otherUserQuery.whereEqualTo("username", "jyo2");
        try {
            userResults = otherUserQuery.find();
        } catch (ParseException e1) {
            // fail
            e1.printStackTrace();
        }

        // get the friend from the query if there was one and add it to the
        // currentUser friends list
        if (userResults.size() != 0) {
            ParseObject currentObject = userResults.get(0);
            friendsList.add(currentObject);
            currentUser.put("friendsList", friendsList);
        }
        // save the update
        try {   
            currentUser.save();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // try to view the friends
        List<ParseObject> currentFL = currentUser.getList("friendsList");
        ParseObject currentPU = currentFL.get(0);
        System.out.println("THIS IS SIZE" + currentFL.size());
        System.out.println(currentPU.get("name"));

2 Answers 2

3

Use the existing "User" class in Parse but subclass it using

@ParseClassName("_User")
public class PUser extends ParseUser {

As per this article. You actually specifically need to refer to the _User "ParseClassName" when subclassing this object. It's super hairy, I was stuck on this for ages because for other classes you only need to "register" the class with Parse using it's literal name as per the Parse Data Browser, which in this case is "User", "Info", "Post" etc, but the User class specifically requires the underscore

Edit Sorry, I should have mentioned, then you simply cast the subclass PUser when you get your returned objects from the query. You may need to change the query to be a User query instead of an object one too.

I know this is an old question, but this wasn't clear for me and there wasn't a complete thread on the issue I had, on SO. Hope this helped.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't think you need to "cast" is to ParseUser, a parseuser is a parseobject as well, apart from having some basic pre-defined properties attached to it, which you can anyways query like you'd query any other parseobject

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.