如何在发出ObjectId(保存)之前引用ParseObject?

时间:2015-04-14 12:23:18

标签: android parse-platform

ParseObject只有在保存后才会得到它ObjectId如果我在创建时处于离线状态且无法保存对象,我如何在仍处于离线状态时引用它?

  

例如,当我还不知道时,如何设置评论的postId   帖子的ID?

我正在考虑在我的扩展TempId中创建一个名为ParseObject的单独属性,我可以将其专有的本地已知ID引用,直到它具有正确的对象ID,但这是我有最好的选择吗?

以下关注我:

  1. 双重处理
  2. 维护所需的所有回调,以确保以正确的顺序更新正确的对象。
  3. 谢谢!

1 个答案:

答案 0 :(得分:0)

这是我的经验法则:思考对象,而不是objectIDs。

var post = // ... this is a real object, not an id. it can be new
var comment = // ... this is a real object, not an id.  it can be new

// posts might have many comments by keeping an array of pointers called "comments"
post.add("comments", comment);

// or posts might have many comments by keeping a relation called "comments"
post.relation("comments").add(comment);

执行此操作后,可以保存关系的父级,并保存未保存的子级:

post.save().then(function(savedPost) {
    // savedPost now has savedPost.id, but who cares?
    // if comments is an array of pointers:
    // savedPost.get("comments") now has comments with ids, but again, we don't need them.

    // if comments is a relation:
    return savedPost.get("comments").query.find();
}).then(function(comments) {
    // comments is an array of objects with ids, but again, if I find
    // myself needing those ids, its a clue that my design is weak
});