我有一个名为“Friends”的表(ParseObject),带有“to”和“from”列,它使用指向User的objectId的指针。此表跟踪谁与谁的朋友,如何创建一个查询,将所有朋友作为用户对象返回 - 而不仅仅是存储在该表中的ID?
谢谢!
ParseUser类经常让我困惑,因为它与ParseObject明显不同,不能与相同的方法一起使用(如ParseQuery.or
)
目前,我正在尝试:
ParseQuery<ParseObject> friendsFrom = ParseQuery.getQuery("Friends");
friendsFrom.whereEqualTo("from", ParseUser.getCurrentUser());
ParseQuery<ParseObject> friendsTo = ParseQuery.getQuery("Friends");
friendsTo.whereEqualTo("to", ParseUser.getCurrentUser());
// where they've been accepted
friendsFrom.whereEqualTo("accepted", true);
friendsTo.whereEqualTo("accepted", true);
// combine the queries - parse does this a 'special' way
List<ParseQuery<ParseObject>> friends = new ArrayList<ParseQuery<ParseObject>>();
friends.add(friendsFrom);
friends.add(friendsTo);
ParseQuery<ParseObject> combinedQuery = ParseQuery.or(friends);
combinedQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> friends, ParseException e) {
if (friends != null) {
Log.d(getClass().getSimpleName(), "Found " + friends.size() + " friends");
contacts.clear();
for (ParseObject friend : friends) {
//show user, or whatever you'd like to do..
}
}
});
但是ParseObject不能转换为ParseUser,所以我在这里做不了什么..
根据罗伯特的建议,我已经尝试了以下内容,但它仍然没有向我提供我需要的UserObjects - 并且出于某种原因,我的日志不是在Parse的这些“findInBackground”回调期间编写的。
ParseQuery < ParseObject > friendshipAndUserQuery = ParseQuery.getQuery("friends");
friendshipAndUserQuery.whereEqualTo("from", ParseUser.getCurrentUser().getObjectId());
friendshipAndUserQuery.include("to");
friendshipAndUserQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject friendship : objects) {
UserObject friend = (UserObject) friendship.getParseObject("to");
contacts.add(friend);
}
}
});