问题
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