public enum RealmCollectionChange<T>

RealmCollectionChange is passed to the notification blocks for Realm collections, and reports the current state of the collection and what changes were made to the collection since the last time the notification was called.

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 converting to index paths in the appropriate section. For example, for a simple one-section table view, you can do the following:

self.notificationToken = results.addNotificationBlock { changes 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( { NSIndexPath(forRow: $0, inSection: 0) }, withRowAnimation: .Automatic) self.tableView.deleteRowsAtIndexPaths( { NSIndexPath(forRow: $0, inSection: 0) }, withRowAnimation: .Automatic) self.tableView.reloadRowsAtIndexPaths( { 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 } }

  • The initial run of the query has completed (if applicable), and the collection can now be used without performing any blocking work.



    case Initial(T)
  • 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.



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



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


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


    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 with details. Currently the only thing that can fail is opening the Realm on a background worker thread to calculate the change set.



    case Error(NSError)