Class

PHFetchResultChangeDetails

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

Declaration

@interface PHFetchResultChangeDetails<__covariant ObjectType> : NSObject

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 cellForItemAtIndexPath: rather than telling UICollectionView to reload.

@property (nonatomic, strong) UICollectionView* collectionView;
// ...
PHFetchResultChangeDetails* fetchResultChangeDetails;
NSIndexSet* deletedCollectionIndicesBeforeChanges;
NSIndexSet* insertedCollectionIndicesAfterDeletions;
NSArray* deletedPhotoPathsBeforeChanges;
NSArray* insertedPhotoPathsAfterDeletions;

[self.collectionView performBatchUpdates:^{
    if (deletedCollectionIndicesBeforeChanges.count > 0) {
        [self.collectionView deleteSections:deletedCollectionIndicesBeforeChanges];
    }
    if (insertedCollectionIndicesAfterDeletions.count > 0) {
        [self.collectionView insertSections:insertedCollectionIndicesAfterDeletions];
    }
    if (deletedPhotoPathsBeforeChanges.count > 0) {
        [self.collectionView deleteItemsAtIndexPaths:deletedPhotoPathsBeforeChanges];
    }
    if (insertedPhotoPathsAfterDeletions) {
        [self.collectionView insertItemsAtIndexPaths:insertedPhotoPathsAfterDeletions];
    }
} completion:nil];

[fetchResultChangeDetails.changedIndexes enumerateIndexesUsingBlock:^(NSUInteger index, BOOL* stop) {
    NSIndexPath* indexPath = [NSIndexPath indexPathWithItem:index inSection:0];
    UICollectionViewCell* changedCell = [self.collectionView cellForItemAtIndexPath:indexPath];
    if (changedCell) {
        // ... Configure changedCell here ...
    }
    // Set *stop = YES to stop iteration early.
}];

Topics

Getting the Changed Fetch Result

fetchResultBeforeChanges

The original fetch result, without recent changes.

fetchResultAfterChanges

The current fetch result, incorporating recent changes.

Getting Change Information

hasIncrementalChanges

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

removedIndexes

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

removedObjects

The items that have been removed from the fetch result.

insertedIndexes

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

insertedObjects

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

changedIndexes

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

changedObjects

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

hasMoves

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

- enumerateMovesWithBlock:

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

Comparing Fetch Results

+ changeDetailsFromFetchResult:toFetchResult:changedObjects:

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

Relationships

Inherits From

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.

- registerChangeObserver:

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

- unregisterChangeObserver:

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

PHPhotoLibraryChangeObserver

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

PHChange

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

PHObjectChangeDetails

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