查找用户名以myString开头的所有以下用户

时间:2014-12-21 03:50:31

标签: android parse-platform

这些是我的关注表的列:

user (Pointer _User)
follower (Pointer _User)

我想做的是让所有用户

follower = currentUser
user.username begins with a certain string

我知道在低级数据库(如mySQL)中,这些数据都可以通过单个查询获取。 解析时有可能吗?如果没有,那么做这种事的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

假设您的currentUser对象是PFUser

注意......我是在objective-c中使用解析程序,所以我不是100%确定这会运行。但我觉得这就是你要找的......你必须先查询所有追随者follower字段等于当前用户。

然后将该查询与用户查询相关联,该用户查询的用户名或名称等于您传入的字符串... 提防 您传入的字符串查询区分大小写...我认为......

因此,如果您的应用中的用户搜索BOB并且数据库已存储Bob ...则查询将无法返回Bob ...它将不会返回任何内容....

ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereEqualTo("username", certainString);

ParseQuery<ParseObject> followerQuery = ParseQuery.getQuery("Followers");
followerQuery.whereEqualTo("follower", ParseUser.getCurrentUser());
followerQuery.matchesKeyInQuery("user", "follower", userQuery);
userQuery.findInBackground(new FindCallback<ParseObject>() {
  public void done(List<ParseObject> list, ParseException e) {
   // comments now contains the comments for posts with images.
  }
});

编辑 通过Parse的Anypic项目做你想做的事......我觉得这是不可能的......你必须要首先查询其followers项为当前用户的关注者表中的用户。然后找到对象。返回结果后,遍历contains string中包含其名称的对象。

ParseQuery<ParseObject> followerQuery = ParseQuery.getQuery("Followers");
followerQuery.whereEqualTo("username", certainString);
followerQuery.whereNotEqualTo("username", ParseUser.getCurrentUser().username);
followerQuery.findInBackground(new FindCallback<ParseObject>() {
  public void done(List<ParseObject> list, ParseException e) {
    for (ParseObject element : list) {
      if (element.user == ParseUser.getCurrentUser()) {
        //This is a follower of the current user... append to a list object or whatever
      }
    }
    // update a label or table once the iteration is done.
  }
});

在一个查询中你仍然可以找到一个可靠的解决方案,但我现在无法想到它...如果我这样做,我会回到这篇文章。