Generic Class

PHFetchResultChangeDetails

A description of changes that occurred in the set of asset or collection objects listed in a fetch result.

Declaration

class PHFetchResultChangeDetails<ObjectType> : NSObject where ObjectType : PHObject

Overview

A PHFetchResultChangeDetails object provides detailed information about the differences between two fetch results—one that you previously obtained and an updated one that would result if you performed the same fetch again. The change details object provides information useful for updating a UI that lists the contents of a fetch result, such as the indexes of added, removed, and rearranged objects.

Processing Changes in Order

PhotoKit describes changedIndexes in the after state, while UICollectionView's performBatchUpdates(_:completion:) expects them in the before state. As a result, changedIndexes can't be used safely inside performBatchUpdates(_:completion:).

Instead, use changedIndexes after and outside the performBatchUpdates(_:completion:) call, reapplying the code used to configure cells in cellForItem(at:) rather than telling UICollectionView to reload.

let collectionView = UICollectionView()
let fetchResultChangeDetails = PHFetchResultChangeDetails()
let deletedCollectionIndicesBeforeChanges = IndexSet()
let insertedCollectionIndicesAfterDeletions = IndexSet()
let deletedPhotoPathsBeforeChanges: [IndexPath] = []
let insertedPhotoPathsAfterDeletions: [IndexPath] = []

collectionView.performBatchUpdates({ () -> Void in
    if (!deletedCollectionIndicesBeforeChanges.isEmpty) {
        collectionView.deleteSections(deletedCollectionIndicesBeforeChanges)
    }
    if (!insertedCollectionIndicesAfterDeletions.isEmpty) {
        collectionView.insertSections(insertedCollectionIndicesAfterDeletions)
    }
    if (!deletedPhotoPathsBeforeChanges.isEmpty) {
        collectionView.deleteItems(at: deletedPhotoPathsBeforeChanges)
    }
    if (!insertedPhotoPathsAfterDeletions.isEmpty) {
        collectionView.insertItems(at: insertedPhotoPathsAfterDeletions)
    }
}, completion:nil)

guard let changeIndices = fetchResultChangeDetails.changedIndexes else {
    return
}
for indexSetElem in changeIndices {
    let indexPath = IndexPath(item: indexSetElem, section: 0)
    guard let changedCell = collectionView.cellForItem(at: indexPath) else {
        break
    }
    // ... Configure changedCell here ...
}

Topics

Getting the Changed Fetch Result

var fetchResultBeforeChanges: PHFetchResult<ObjectType>

The original fetch result, without recent changes.

var fetchResultAfterChanges: PHFetchResult<ObjectType>

The current fetch result, incorporating recent changes.

Getting Change Information

var hasIncrementalChanges: Bool

A Boolean value that indicates whether changes to the fetch result can be described incrementally.

var removedIndexes: IndexSet?

The indexes from which objects have been removed from the fetch result.

var removedObjects: [ObjectType]

The items that have been removed from the fetch result.

var insertedIndexes: IndexSet?

The indexes where new objects have been inserted in the fetch result.

var insertedObjects: [ObjectType]

The new items that have been inserted in the fetch result.

var changedIndexes: IndexSet?

The indexes of objects in the fetch result whose content or metadata have been updated.

var changedObjects: [ObjectType]

The objects in the fetch result whose content or metadata have been updated.

var hasMoves: Bool

A Boolean value that indicates whether objects have been rearranged in the fetch result.

func enumerateMoves((Int, Int) -> Void)

Runs the specified block for each case where an object has moved from one index to another in the fetch result.

Comparing Fetch Results

init(from: PHFetchResult<ObjectType>, to: PHFetchResult<ObjectType>, changedObjects: [ObjectType])

Creates a change details object that summarizes the differences between two fetch results.

Relationships

Inherits From

Conforms To

See Also

Observing Changes to the Photo Library

Observing Changes in the Photo Library

Register an observer to be notified of changes to the photo library.

func register(PHPhotoLibraryChangeObserver)

Registers an object to receive messages when objects in the photo library change.

func unregisterChangeObserver(PHPhotoLibraryChangeObserver)

Unregisters an object so that it no longer receives change messages.

protocol PHPhotoLibraryChangeObserver

A protocol you can implement to be notified of changes that occur in the photo library.

class PHChange

A description of a change that occurred in the photo library.

class PHObjectChangeDetails

A description of changes that occurred in an asset or collection object.