ParseObject pinInBackground returning null objectId

China☆狼群 提交于 2021-02-07 19:54:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/25289755/parseobject-pininbackground-returning-null-objectid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!