List<ParseUser>.contains(ParseUser)始终返回FALSE的问题排查
I’ve run into this exact issue before with Parse on Android—let’s break down why this happens and how to fix it.
First, let’s remember how ParseObject (which ParseUser extends) handles equality: two ParseObjects are considered equal only if they have the same class name and identical objectId values. The default contains() method relies on this equality check, so if it’s returning false even when you see the user in the list, one of a few key things is going wrong.
Common Causes & Fixes
1. Your search results lack a valid objectId
If you used selectKeys() in your search query to fetch only specific fields (like username), you might have excluded the objectId by accident. Without an objectId, the equality check will always fail, even if the username matches.
Fix:
Either remove the selectKeys() call entirely, or explicitly include objectId in the list of keys to fetch:
ParseQuery<ParseUser> searchQuery = ParseUser.getQuery(); // Explicitly include objectId if using selectKeys searchQuery.selectKeys(Arrays.asList("objectId", "username", "profilePicture"));
2. The friendsArray/pendingFriendsArray pointers aren’t fully loaded
If you’re using a cached version of ParseUser.getCurrentUser(), the pointer fields (friendsArray and pendingFriendsArray) might be lazy-loaded—meaning the ParseUser objects in those lists only have their objectId stored locally, but sometimes cached objects can have stale metadata that disrupts the equality check.
Fix:
Force-fetch the current user to ensure all pointer fields are loaded before checking:
ParseUser.getCurrentUser().fetchIfNeededInBackground(new GetCallback<ParseUser>() { @Override public void done(ParseUser currentUser, ParseException e) { if (e == null) { List<ParseUser> friends = currentUser.getList("friendsArray"); // Now perform your contains() check here } else { // Handle fetch error } } });
3. The default equality check isn’t working as expected (fallback solution)
If you’ve verified that the objectIds match but contains() still fails (rare, but possible with older Parse SDK versions), bypass the default contains() method and manually check for matching objectIds.
Fix:
Create a helper method to compare users by their objectId:
private boolean doesUserListContainUser(List<ParseUser> userList, ParseUser targetUser) { // Guard against nulls if (targetUser == null || targetUser.getObjectId() == null || userList == null) { return false; } for (ParseUser user : userList) { if (targetUser.getObjectId().equals(user.getObjectId())) { return true; } } return false; }
Then use this instead of friendsArray.contains(user).
First Step to Debug
Before trying any fixes, print out the objectIds of the users in both lists to confirm they match:
// Log friendsArray users for (ParseUser friend : ParseUser.getCurrentUser().getList("friendsArray")) { Log.d("ParseDebug", "Friend ID: " + friend.getObjectId()); } // Log search results users for (ParseUser result : searchResultsList) { Log.d("ParseDebug", "Search Result ID: " + result.getObjectId()); }
If the IDs match, the fallback helper method will definitely work. If they don’t, you’ll know your search query or friend list loading is missing something.
内容的提问来源于stack exchange,提问作者Brandon Stillitano

