RealmCollectionChange

public enum RealmCollectionChange<T>

A RealmCollectionChange value encapsulates information about changes to collections that are reported by Realm notifications.

The change information is available in two formats: a simple array of row indices in the collection for each type of change, and an array of index paths in a requested section suitable for passing directly to UITableView’s batch update methods.

The arrays of indices in the .Update case follow UITableView’s batching conventions, and can be passed as-is to a table view’s batch update functions after being converted to index paths. For example, for a simple one-section table view, you can do the following:

self.notificationToken = results.addNotificationBlock { changes in
    switch changes {
    case .initial:
        // Results are now populated and can be accessed without blocking the UI
        self.tableView.reloadData()
        break
    case .update(_, let deletions, let insertions, let modifications):
        // Query results have changed, so apply them to the TableView
        self.tableView.beginUpdates()
        self.tableView.insertRowsAtIndexPaths(insertions.map { NSIndexPath(forRow: $0, inSection: 0) },
           withRowAnimation: .Automatic)
        self.tableView.deleteRowsAtIndexPaths(deletions.map { NSIndexPath(forRow: $0, inSection: 0) },
           withRowAnimation: .Automatic)
        self.tableView.reloadRowsAtIndexPaths(modifications.map { NSIndexPath(forRow: $0, inSection: 0) },
           withRowAnimation: .Automatic)
        self.tableView.endUpdates()
        break
    case .error(let err):
        // An error occurred while opening the Realm file on the background worker thread
        fatalError("\(err)")
        break
    }
}
  • .initial indicates that the initial run of the query has completed (if applicable), and the collection can now be used without performing any blocking work.

    Declaration

    Swift

    case initial(T)
  • .update indicates that a write transaction has been committed which either changed which objects are in the collection, and/or modified one or more of the objects in the collection.

    All three of the change arrays are always sorted in ascending order.

    Declaration

    Swift

    case update(T, deletions: [Int], insertions: [Int], modifications: [Int])

    Parameters

    deletions

    The indices in the previous version of the collection which were removed from this one.

    insertions

    The indices in the new collection which were added in this version.

    modifications

    The indices of the objects in the new collection which were modified in this version.

  • If an error occurs, notification blocks are called one time with a .error result and an NSError containing details about the error. This can only currently happen if the Realm is opened on a background worker thread to calculate the change set.

    Declaration

    Swift

    case error(Swift.Error)