2
votes

I'm clearly missing something. I started reading the Parse.com guide, and saw this:

ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.saveInBackground();

Which creates this object:

objectId: "xWMyZ4YEGZ", score: 1337, playerName: "Sean Plott", cheatMode: false,
createdAt:"2011-06-10T18:33:42Z", updatedAt:"2011-06-10T18:33:42Z"

And then it says -

If you have the objectId, you can retrieve the whole ParseObject using a ParseQuery

So I have two questions -

1. How can I get the objectId when it's created on the parse-server-side?

2. If I'm not creating the objectId, then I cannot re-create it if it's lost (user clears app's data for example). So there's no real point to remote server.. What am I missing here?

1

1 Answers

5
votes

There is an overload of the saveInBackground method which takes a SaveCallback object. In the savecallback's done method, you should be able to getObjectId() the original ParseObject and would give you the generated objectId from the server.

Take a look at this:

final ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);

gameScore.saveInBackground(new SaveCallback() {
    public void done(ParseException e) {
        if (e == null) {
            // if null, it means the save has succeeded
            String id = gameScore.getObjectId(); // Here you go
        } else {
            // the save call was not successful.
        }
    }
}

Now for your second question.

It looks like you're trying to save some kind of player's progress. If you add a Pointer column to your game score objects, you can easily retrieve that game score data since your User identity wouldn't be changing even after re-logging in.

edit: sample query

final ParseQuery query = new ParseQuery("GameScore");
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback() {