3

I have a block of code in an Android project that creates a ParseObject and stores it in the local datastore. However, when I go to check the objectId in the done() callback method of pinInBackground() it returns null. If I switch from pinInBackground() to saveInBackground() then it works fine and a valid objectId is given. Here is the code:

final ParseObject testObject = new ParseObject("TestObject");

testObject.put("foo", "bar2");
testObject.pinInBackground(new SaveCallback() {
  @Override
  public void done(ParseException e) {
      if (e != null) {
          Log.e(TAG, "Failed to pin TestObject: " + e.getMessage());
          return;
      }

      Log.d(TAG, "Added key-value pair to TestObject '" + testObject.getObjectId() + "': 'foo' => 'bar2'");
  }

});

The log shows:

Added key-value pair to TestObject 'null': 'foo' => 'bar2'

Why is the objectId null? Do I need to set it since it's not saving to the cloud?

Zac
  • 2,325
  • 3
  • 23
  • 33

1 Answers1

6

The problem is that you are not saving object to the Parse database, only to the device, so the object has not actually been created yet. pinInBackground() is only saving to the device, while saveInBackground() is saving to the Parse database, thereby, actually creating the object. If you use .saveEventually(), that will pin until the network can be reached to save to the Parse database. But in general - you will not actually create an object by only saving to your device, it must be saved to the Parse database to create.

user2163298
  • 665
  • 4
  • 6
  • So, should I call setObjectId() then? I'm planning on using Parse for storing all data in the it's local datastore until a user signs up for a subscription service, then I would let my app call saveEventually(). – Zac Aug 14 '14 at 02:55
  • I'm not so sure that's the best strategy - only saving to the local datastore means your data won't be available over the network to other users of the app - you can still store information to the database even before a user signs up. you can use Parse Anonymous Users if you want to differentiate between a User who has signed up and a user who hasn't. – user2163298 Aug 14 '14 at 13:48