0

When the user clicks on the "find random opponent" button, I would like the User class in Parse to create a new field (called readyToPlay) and set the value to true. Then, start a new activity where the user is randomly matched with other users whose variable readyToPlay is also set to true.

The problem is when I click the "find random opponent" button, no new field is created. I looked at the Parse documentation and cant figure out what I am doing wrong. I have attached the relevant code below.

public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, FindRandomOpponent.class);
            ParseUser currentUser = ParseUser.getCurrentUser();
            currentUser.put(ParseConstants.KEY_READY_TO_PLAY, true);
            currentUser.saveInBackground()
            startActivity(intent);
        } 

Edit1: I called the saveInBackground method, but my user database didn't update.

Edit2: I am currently signed in as a user, so the device does have access to the internet.

2
  • Did you check yes for your apps Allow client class creation in Settings? Commented Sep 15, 2015 at 1:38
  • I checked the settings, it's listed as yes Commented Sep 15, 2015 at 1:47

2 Answers 2

1

This will help. It will check your user is saved or give exception for further investigation.

ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put(ParseConstants.KEY_READY_TO_PLAY, true);
currentUser.saveInBackground(new SaveCallback() {
  @Override
  public void done(ParseException e) {
    if (e == null) {
      Intent intent = new Intent(MainActivity.this, FindRandomOpponent.class);
      startActivity(intent);
    } else {
      //log error
    }
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this , and my console spat out the following: 09-14 19:04:37.450 7181-7181/AppName/Choreographer﹕ Skipped 49 frames! The application may be doing too much work on its main thread. 09-14 19:04:52.760 7181-7181/AppName/MainActivity﹕ 1234 09-14 19:04:52.900 7181-7202/AppName/EGL_emulation﹕ eglSurfaceAttrib not implemented Please note that 1234 is the username that I am currently logged in as. I do not think it offers any clues, do you?
I logged the error with Log.d(TAG, e.getMessage());
I figured out the problem, turned out KEY_READY_TO_PLAY in currentUser.put(ParseConstants.KEY_READY_TO_PLAY) was defined as "Ready to play" as opposed to readyToPlay. What a terrible mistake. Apologies for wasting your time
Thank you for the help though! I will be logging every exception from now.
0

You need to call the saveInBackground() method on your currentUser object.

Reference: Parse Docs on saving objects

2 Comments

I tried this, currentUser.saveInBackground(); It does not work.
Is currentUser null? Also, does the device have internet access (important to check if using an emulator). Also be aware that this method is asynchronous i.e. the code will continue without waiting for the value to be saved.

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.