Which is more expensive, notifyDatasetChanged() or looping over a large cursor?
<p>Basically, I have a RecyclerView with an adapter populating the data from a cursor. I have two options to get item animations:</p>

<ol>
<li><p>Use <code>setHasStableIds(true)</code> and override <code>getItemId</code> to return the ID of the database row, and then use a <code>swapCursor</code> function in the adapter that swaps its current cursor with the new one and then calls <code>notifyDatasetChanged</code></p></li>
<li><p>Similar to option 1, but instead of simply swapping the cursor and calling <code>notifyDatasetChanged</code>, iterate over the old and new cursor, comparing the rows to determine which rows have been added/deleted/changed and keeping track of them, then calling <code>notifyItemAdded</code>, <code>notifyItemChanged</code>, etc. to tell the adapter specifically what's been updated</p></li>
</ol>

<p>With option 1, <code>notifyDatasetChanged</code> will re-bind all the visible items (and some other stuff) which is inefficient, but with option 2 I would have to loop over the full cursor to figure out which items have been added/deleted, and if the cursor is fairly large, this could also take some time. Does anyone have any ideas about which would be the better solution and why?</p>

<p>Edit:</p>

<p>To make things even more interesting, I also have a third option:</p>

<ol start="3">
<li>Similar to option 2, but instead of iterating over every item in the cursor, only iterate over the currently bound items (figured out by checking the <code>RecyclerView</code>'s children) since the others will pick up the new cursor values when they're bound. The only problem with this is that it doesn't work for additions/deletions, so the way around it would be to do a full iteration if <code>oldCursor.count() != newCursor.count()</code>. If the counts are the same, I can do <code>setHasStableIds(false)</code> and then even if the row at a position is different than it was previously, <code>onBindViewHolder</code> will take care of it.</li>
</ol>

<p>Downsides of this would be that the ID's aren't stable, which means the <code>RecyclerView</code> will have to do some extra work in some places</p>