Android - 解析查询:每个用户限制1个对象

时间:2016-04-27 11:41:40

标签: android parse-platform android-recyclerview android-adapter

我有一个消息的Parse查询,现在它返回当前用户跟随的所有消息。

我希望将其限制为每个用户只有1封邮件,因此当用户发送邮件时,它会替换旧邮件。

ParseQuery<ParseObject> receiver = ParseQuery.getQuery("Messages");
        receiver.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK);
        receiver.whereEqualTo(ParseConstants.KEY_RECIPIENT_IDS, ParseUser.getCurrentUser().getObjectId());

        ParseQuery<ParseObject> sender = ParseQuery.getQuery("Messages");
        sender.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK);
        sender.whereEqualTo(ParseConstants.KEY_SENDER_NAME, ParseUser.getCurrentUser().getUsername());



        List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
        queries.add(receiver);
        queries.add(sender);

        ParseQuery<ParseObject> mainQuery = ParseQuery.or(queries);
        mainQuery.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> messages, ParseException e) {


                if (e == null) {

                    // We found messages!
                    mMessages = messages;


                    if (isFragmentUIActive()) {
                        if (recyclerView.getAdapter() == null) {
                            recyclerView.setAdapter(new InboxAdapter(recyclerView.getContext(), mMessages, R.layout.message_item));
                            recyclerView.setHasFixedSize(true);
                            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                            recyclerView.setItemAnimator(new DefaultItemAnimator());
                        } else {
                            // refill the adapter!
                            Log.i("Refill", "todo");
                        }

                    }
                }
            }
        });

1 个答案:

答案 0 :(得分:0)

使用documentation

  

setLimit(int newLimit):控制返回的最大结果数。

代码示例:

ParseQuery<ParseObject> receiver = new ParseQuery<ParseObject>(getQuery());
receiver.setLimit(1);

public ParseQuery<T> getQuery() {
  return query;
}