Pseduosance
Pseduosance

Reputation: 315

Android, Get a pointer to a ParseObject

How do you get a pointer to a parse object?

For example, my ParseUser has a column called "school", which is a pointer to a Parse class "School"

school Pointer<School>

I am able to get a ParseObject of type School, however, I don't know how to get a pointer to this School ParseObject so I can put it in the ParseUser's school field.

I saw similar posts to this one that said to create a parse object without data and give it the correct object ID:

ParseObject mySchoolPtr = ParseObject.createWithoutData("School", mySchoolObject.getObjectId());
currentUser.put("school", mySchoolPtr);

However, after running this code my Parse database for User just lists "(Undefined)" in the school field.

Below is a code snippet capturing this entire process (after the snippet I further explain what is happening in the snippet):

    String school = schoolEditText.getText().toString().trim();
    String password = passwordEditText.getText().toString().trim();

    final ParseUser currentUser = ParseUser.getCurrentUser();

    currentUser.setPassword(password);

    // Query schools and search for name == school, create pointer for that one and put it for user "school"
    ParseQuery<ParseObject> query = ParseQuery.getQuery("School");
    query.whereEqualTo("name", school);
    query.getFirstInBackground(new GetCallback<ParseObject>() {
        public void done(ParseObject school_found, ParseException e) {
            if (school_found != null) {
                Log.d("School", "Retrieved " + school_found.getString("name"));
                ParseObject myschoolptr = ParseObject.createWithoutData("School", school_found.getObjectId());
                currentUser.put("school", myschoolptr);                            
            } else {
                Log.d("School", "Error: School name not found in Database. " + e.getMessage());
            }
        }
    });
    currentUser.saveInBackground();
    showMainActivity();

In my Parse class School, I have a column labeled name. I have the user input their school, for example "MIT". I then query the schools class on parse for an equivalent name. So that gives me the corresponding ParseObject of school class. I want to put that in the ParseUsers school field. However, the ParseUser field for school is a pointer to the School class. How can I get a pointer to my given ParseObject to put into the user's school field?

Upvotes: 3

Views: 1050

Answers (1)

Anyonymous2324
Anyonymous2324

Reputation: 260

You have to call currentUser.saveInBackground(); inside the GetCallback for it to update correctly:

...
query.getFirstInBackground(new GetCallback<ParseObject>() {
    public void done(ParseObject school_found, ParseException e) {
        if (school_found != null) {
            Log.d("School", "Retrieved " + school_found.getString("name"));
            ParseObject myschoolptr = ParseObject.createWithoutData("School", school_found.getObjectId());
            currentUser.put("school", myschoolptr);        
            currentUser.saveInBackground();      
            showMainActivity();              
        } else {
            Log.d("School", "Error: School name not found in Database. " + e.getMessage());
        }
    }
});

Upvotes: 1

Related Questions