如何仅查询新数据(Parse / Android)?

时间:2016-10-04 13:04:58

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

我有以下方法查询主活动的onCreate方法中的项列表并将它们设置为我的适配器:

private void retrieveYeets() {
        String groupId = ParseUser.getCurrentUser().getString("groupId");
        ParseQuery<ParseObject> query = new ParseQuery<>(ParseConstants.CLASS_YEET);
        query.whereContains(ParseConstants.KEY_GROUP_ID, groupId);
        query.orderByDescending("lastReplyUpdatedAt");
        query.setLimit(1000);
        query.findInBackground((yeets, e) -> {

            if (mSwipeRefreshLayout.isRefreshing()) {
                mSwipeRefreshLayout.setRefreshing(false);
            }

            if (e == null) {

                // We found messages!
                mYeets = yeets;
                /*System.out.println(yeets);*/

                FeedAdapter adapter = new FeedAdapter(getContext(), yeets);
                adapter.setHasStableIds(true);
                mRecyclerView.setHasFixedSize(true);
                adapter.notifyDataSetChanged();
                mRecyclerView.setAdapter(adapter);
            }
        });
    }

onBindViewHolder 中,我有一个方法可以从指针中检索其他数据,包括帖子作者的用户名,全名和个人资料图片:

private void downloadProfilePicture(ViewHolder holder, ParseObject yeet) {
        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.whereEqualTo(ParseConstants.KEY_OBJECT_ID, yeet.getParseObject(ParseConstants.KEY_SENDER_AUTHOR_POINTER).getObjectId());
        query.findInBackground((user, e) -> {
            if (e == null) for (ParseObject userObject : user) {

                if (userObject.getParseFile("profilePicture") != null) {
                    String profilePictureURL = userObject.getParseFile("profilePicture").getUrl();

                    // Asynchronously display the profile picture downloaded from Parse
                    if (profilePictureURL != null) {

                        Picasso.with(mContext)
                                .load(profilePictureURL)
                                .networkPolicy(NetworkPolicy.OFFLINE)
                                .placeholder(R.color.placeholderblue)
                                .fit()
                                .into(holder.profilePicture);

                    } else {
                        holder.profilePicture.setImageResource(Integer.parseInt(String.valueOf(R.drawable.ic_profile_pic_add)));
                    }
                }

                if (!(userObject.getString(ParseConstants.KEY_AUTHOR_FULL_NAME).isEmpty())) {
                    holder.fullName.setText(userObject.getString(ParseConstants.KEY_AUTHOR_FULL_NAME));
                } else {
                    holder.fullName.setText("Anonymous Lose");
                }

                holder.username.setText(userObject.getString(ParseConstants.KEY_USERNAME));

            }
        });
    }

重要部分如下:在SwipeRefreshLayout刷新时调用 retrieveYeets()。所以每当我刷新时,我都会重新检索所有数据。

只需看看Twitter的用户界面刷新,看看我的意思。我只想检索尚未提取的新数据,并将其添加到我的适配器顶部。否则,我正在执行不必要的查询,并且个人资料图片,用户名和全名在整个Feed中闪烁,因为每次刷新都会使用新查询重新下载。

目的:

  • 在最后创建的对象的日期之后查询对象
  • 仅将新数据附加到现有适配器

更新

private void refreshYeets(Date date, FeedAdapter adapter) {
        String groupId = ParseUser.getCurrentUser().getString("groupId");
        ParseQuery<ParseObject> query = new ParseQuery<>(ParseConstants.CLASS_YEET);
        query.whereContains(ParseConstants.KEY_GROUP_ID, groupId);
        query.orderByDescending("lastReplyUpdatedAt");
        if (date != null)
            query.whereLessThanOrEqualTo("createdAt", date);
        query.setLimit(1000);
        query.findInBackground((yeets, e) -> {

            if (mSwipeRefreshLayout.isRefreshing()) {
                mSwipeRefreshLayout.setRefreshing(false);
            }

            if (e == null) {
                // We found messages!
                mYeets.addAll(0, yeets); //This should append new messages to the top
                /*System.out.println(yeets);*/
                if (mRecyclerView.getAdapter() == null) {
                    adapter.setHasStableIds(true);
                    mRecyclerView.setHasFixedSize(true);
                    adapter.notifyDataSetChanged();
                    mRecyclerView.setAdapter(adapter);
                } else {
                    adapter.notifyDataSetChanged();
                }

            }
        });
    }

这可以工作,但许多适配器列表对象被替换并处于错误的位置。我需要在这里进行什么样的修正?不知何故需要按列表偏移新对象的数量?

1 个答案:

答案 0 :(得分:-1)

以下是您可以这样做的方式:

private void retrieveYeets(Date date) {
        String groupId = ParseUser.getCurrentUser().getString("groupId");
        ParseQuery<ParseObject> query = new ParseQuery<>(ParseConstants.CLASS_YEET);
        query.whereContains(ParseConstants.KEY_GROUP_ID, groupId);
        query.orderByDescending("lastReplyUpdatedAt");
        if( date != null )
         query. whereGreaterThan("createdAt", date);
        query.setLimit(1000);
        query.findInBackground((yeets, e) -> {

            if (mSwipeRefreshLayout.isRefreshing()) {
                mSwipeRefreshLayout.setRefreshing(false);
            }

            if (e == null) {

                // We found messages!
                mYeets.addAll(0, yeets); //This should append new messages to the top
                /*System.out.println(yeets);*/
                if( mRecyclerView.getAdapter() == null ) {
                mAdapter = new FeedAdapter(getContext(), mYeets);
                mAdapter.setHasStableIds(true);
                mRecyclerView.setHasFixedSize(true);
                adapter.notifyDataSetChanged();
                mRecyclerView.setAdapter(adapter);
                }
                else {
                mAdapter.notifyDataSetChanged();
                }

            }
        });
    }