Search code examples
javaandroidparse-platformlistadapter

Order a ParseQuery by the date at which only one field has been updated


I have a feed containing posts that are currently ordered by "updatedAt". My original intention was to push up posts to the top of the feed which were last replied to (I do this by incrementing a "replyCount" field in each post when a user leaves a comment), not totally cognizant of the fact that another field, "likeCount" is also being updated when user's "like" a post. I would still like to push those posts that were recently "replied to" to the top of the feed, but do not at the expense of the weird UX behavior that associated with liking posts pushing them up as well. I'm not sure how to separate the two.

What should I do here? Can I maybe add another column called "lastReplyCountTime" and then sort queries based on that? Maybe set lastReplyCountTime for all posts to the current time when saved to the database, and then only update that value when a post receives a reply?

        String groupId = ParseUser.getCurrentUser().getString("groupId");
        ParseQuery<ParseObject> query = new ParseQuery<>(ParseConstants.CLASS_POST);
        query.whereContains(ParseConstants.KEY_GROUP_ID, groupId);
        /*query.addDescendingOrder(ParseConstants.KEY_CREATED_AT);*/
        query.orderByDescending("updatedAt");
        query.findInBackground((posts, e) -> {

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

            if (e == null) {

                // We found messages!
                mPosts = posts;

                String[] usernames;
                usernames = new String[mPosts.size()];
                int i = 0;
                for(ParseObject post : mPosts) {
                    usernames[i] = yeet.getString(ParseConstants.KEY_SENDER_NAME);
                    i++;
                }

                FeedAdapter adapter = new FeedAdapter(
                        getListView().getContext(),
                        mYeets);
                setListAdapter(adapter);

            }
        });
    }

Solution

  • You have 2 options:

    1. Like you suggested you can create another date property let's call it sortedUpdatedAt and update it with the current date each time you are updating the relevant values
    2. If you still want to use updatedAt you can wrap your like object in a separate parse object (class). This class will be saved as a relation in the parent class and then each time the user "like" you can just update only this class and not the whole object. This way the updatedAt of your parent object will not be changed.

    I think that option 1 is great since it's not so complicated and you can do it very quick.