我显然错过了一些东西。 我开始阅读 Parse.com 指南,并看到了这个:
ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.saveInBackground();
它创建了这个对象:
objectId: "xWMyZ4YEGZ", score: 1337, playerName: "Sean Plott", cheatMode: false,
createdAt:"2011-06-10T18:33:42Z", updatedAt:"2011-06-10T18:33:42Z"
然后它说 -
If you have the objectId, you can retrieve the whole ParseObject using a ParseQuery
所以我有两个问题 -
1.在解析服务器端创建 objectId 时如何获取它?
2.如果我没有创建 objectId,那么如果它丢失(例如用户清除应用程序的数据),我将无法重新创建它。所以远程服务器没有真正的意义。我在这里错过了什么?
最佳答案
saveInBackground
方法存在重载,该方法采用 SaveCallback
对象。在 savecallback 的 did 方法中,您应该能够获取原始 ParseObject 的 getObjectId() 并为您提供从服务器生成的 objectId。
看看这个:
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.
}
}
}
现在回答第二个问题。
看起来您正在尝试保存某种玩家的进度。如果您向游戏分数对象添加指针列,您可以轻松检索该游戏分数数据,因为即使重新登录后您的用户身份也不会改变。
编辑:示例查询
final ParseQuery query = new ParseQuery("GameScore");
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback() {
关于android - 获取ParseObject的objectId并保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26676661/