Bug in sample code in Realm documentation


#1

The sample code at https://realm.io/docs/swift/latest#collection-notifications has a bug. In the .update case in the observer, the table view is updated with insertions first, deletions second, and modifications third. However, the .update case clearly shows deletions as the first parameter, and there are good reasons for this.

Assume a deletion takes place at index M and an insertion takes place at index N.

If M < N, the insertion will put the new record one row before where it really should be, so if the records at N and N+1 happen to be on screen at the time, the record at N+1 will get displayed twice.

If M == N, the deletion will cancel out the insertion. If the affected index happens to be on screen at the time, the deleted record will still be displayed.

If M > N, the deletion will be applied to the wrong record, i.e. to record M - 1 because of the premature insertion of the record at N.

The documentation should be updated to state very clearly that deletions, insertions, and modifications should be processed in exactly that order.


#2

@KDean I put this in our docs backlog for tracking - thank you