关系查询 Parse.com android
Relational Query Parse.com android
所以我正在使用 Parse.com 作为后端实现一个应用程序,Parse 中基本上有 3 个 class。第一个 User 我有用户信息,Gallery 我有图片“比如 Instagram,最后 Follow Class 其中我有 following 和 followers 的关系。
现在我有一个 activity,我必须只为我关注的人显示所有图像。我无法编写正确的关系查询来获得正确的结果。
所以这是显示 所有 数据库中图像的代码 不仅针对我关注的人。
public class FollowingImages extends ParseQueryAdapter<ParseObject> {
public FollowingImages (Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Gallery");
query.include("createdBy");
return query;
}
});
}
// Customize the layout by overriding getItemView
@Override
public View getItemView(final ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.list_item, null);
}
super.getItemView(object, v, parent);
// retrieving the Images from Parse.com
return v;
}
我不知道应该在构造函数中还是在 getItemView 中的什么位置应用关系查询。
如有任何帮助,我们将不胜感激。
您可以使用 matchesKeyInQuery 约束方法。
You can use the matchesKeyInQuery method to get objects where a key matches the value of a key in a set of objects resulting from another query.
(在 parse docs 之后)
所以在你的情况下(我是手写的)它可能是这样的:
ParseQuery<Follow> whoDoIFollow = ParseQuery.getQuery("Follow")
.select("From")
.whereEqualTo("To", <user>);
ParseQuery<Gallery> theirImages = ParseQuery.getQuery("Gallery")
.whereMatchesKeyInQuery("createdBy", "From", whoDoIFollow);
如果我是正确的,从数据传输的角度来看,这应该是最佳方式,因为一切都发生在服务器端,您只能得到结果(图像)。
只需更换
public FollowingImages (Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery<ParseObject> whoDoIFollow = ParseQuery.getQuery("Follow")
.select("From")
.whereEqualTo("To", <user>);
ParseQuery<ParseObject> theirImages = ParseQuery.getQuery("Gallery")
.whereMatchesKeyInQuery("createdBy", "From", whoDoIFollow);
return theirImages;
}
});
}
您需要对查询应用约束。
ParseQuery<ParseObject> query = ParseQuery.getQuery("Gallery");
query.whereEqualTo("createdBy", "theUserId");
query.findInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("score", "The find request failed.");
} else {
Log.d("score", "Retrieved the object.");
}
}
});
查看官方guide了解更多详情。
所以我正在使用 Parse.com 作为后端实现一个应用程序,Parse 中基本上有 3 个 class。第一个 User 我有用户信息,Gallery 我有图片“比如 Instagram,最后 Follow Class 其中我有 following 和 followers 的关系。
现在我有一个 activity,我必须只为我关注的人显示所有图像。我无法编写正确的关系查询来获得正确的结果。
所以这是显示 所有 数据库中图像的代码 不仅针对我关注的人。
public class FollowingImages extends ParseQueryAdapter<ParseObject> {
public FollowingImages (Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Gallery");
query.include("createdBy");
return query;
}
});
}
// Customize the layout by overriding getItemView
@Override
public View getItemView(final ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.list_item, null);
}
super.getItemView(object, v, parent);
// retrieving the Images from Parse.com
return v;
}
我不知道应该在构造函数中还是在 getItemView 中的什么位置应用关系查询。
如有任何帮助,我们将不胜感激。
您可以使用 matchesKeyInQuery 约束方法。
You can use the matchesKeyInQuery method to get objects where a key matches the value of a key in a set of objects resulting from another query.
(在 parse docs 之后)
所以在你的情况下(我是手写的)它可能是这样的:
ParseQuery<Follow> whoDoIFollow = ParseQuery.getQuery("Follow")
.select("From")
.whereEqualTo("To", <user>);
ParseQuery<Gallery> theirImages = ParseQuery.getQuery("Gallery")
.whereMatchesKeyInQuery("createdBy", "From", whoDoIFollow);
如果我是正确的,从数据传输的角度来看,这应该是最佳方式,因为一切都发生在服务器端,您只能得到结果(图像)。
只需更换
public FollowingImages (Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery<ParseObject> whoDoIFollow = ParseQuery.getQuery("Follow")
.select("From")
.whereEqualTo("To", <user>);
ParseQuery<ParseObject> theirImages = ParseQuery.getQuery("Gallery")
.whereMatchesKeyInQuery("createdBy", "From", whoDoIFollow);
return theirImages;
}
});
}
您需要对查询应用约束。
ParseQuery<ParseObject> query = ParseQuery.getQuery("Gallery");
query.whereEqualTo("createdBy", "theUserId");
query.findInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("score", "The find request failed.");
} else {
Log.d("score", "Retrieved the object.");
}
}
});
查看官方guide了解更多详情。