Search code examples
javaandroidduplicatesandroid-cursor

How to remove duplicate rows in a Cursor (Android SDK)?


Currently, I have made 3 queries (resulting in 3 cursors), and then I merged the cursors using the MergeCursor class. However, this has caused duplicates in the cursor, and I can't seem to find a way to remove them? What would be the ideal method to fix this problem?


Solution

  • A Cursor is an object tied to the ResultSet, not the data therein. If the three result-sets have identical keys, their primary-keys will need to be fetched to de-duplicate the rows - the Cursor implementation does not provide this function. There are several options, two named here:

    1. As eluded to in an earlier comment - do this server-side and have the joined result returned. Ex: Send the base query from the client, have the server start three queries and merge the result - though databases excel in set-operations and there is almost never a performance gain in doing this programatically.
    2. Launch one task that in turn runs the three queries and does the work to fetch the rows, returning just the distinct set of keys.