简体   繁体   English

如何在parse.com中使用关系字段

[英]How to use Relation fields in parse.com

I'm using parse.com , and I just want to know how to set and get relational data in a ParseObject Subclass, for example like this . 我正在使用parse.com ,我只想知道如何在ParseObject子类中设置和获取关系数据,例如this

Can you please give an example of field of type Relation and show me how to set and get it in the subclass? 您可以举一个类型为Relation的字段的示例,并向我展示如何在子类中进行设置和获取吗?

Thanks so much in advance ! 提前非常感谢!

I'm not sure about what you are really asking. 我不确定你到底在问什么。 Relation fields are sets of pointers to other ParseObject . Relation字段是指向其他ParseObject的指针集。 You don't have to add convenience methods in the subclass if you don't need to. 如果不需要,您不必在子类中添加便捷方法。 The super class ParseObject has all the methods necessary to interact with relation fields. 超类ParseObject具有与关系字段进行交互所必需的所有方法。 The main entry point is getRelation("columnName") , which you can use on any ParseObject instance. 主要入口是getRelation("columnName") ,可以在任何ParseObject实例上使用它。

Let's say you have a AnywallPost class and you have set a relation column, "likes" , storing all ParseUser s that like that post. 假设您有一个AnywallPost类,并且设置了一个关系列"likes" ,用于存储所有喜欢该帖子的ParseUser Documentation is pretty clear in explaining how to get/set. 文档很清楚地解释了如何获取/设置。

Set

You don't really set anything, you just add a new item in the relation field. 您实际上没有设置任何内容,只需在关系字段中添加一个新项目。

ParseRelation<ParseUser> relation = anywallPost.getRelation("likes");
relation.add(parseUser1);
relation.add(parseUser2);    
anywallPost.saveInBackground();

A convenient method inside your subclass could be add(ParseUser user) : 子类内部的便捷方法可以是add(ParseUser user)

public void add(ParseUser user) {
    ParseRelation<ParseUser> relation = this.getRelation("likes");
    relation.add(user);
    this.saveInBackground();
}

Then you can simply call anywallPost.add(parseUser) . 然后,您可以简单地调用anywallPost.add(parseUser)

Get 得到

You don't really get anything, but rather you find items inside the relation. 您并没有真正得到任何东西,而是找到了关系中的项目。 This is honestly well documented in the official docs . 老实说,这在官方文档中有很好的记录。 A useful method inside your subclass could give you the query: 子类内部的一个有用方法可以为您提供查询:

public ParseQuery<ParseUser> getLikes() {
    return this.getRelation("likes").getQuery();
}

Then you can use ParseQuery<ParseUser> q = anywallPost.getLikes() and use the query as you wish. 然后,您可以使用ParseQuery<ParseUser> q = anywallPost.getLikes()并根据需要使用查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM