Article

Observing Changes in the Photo Library

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

Overview

To be notified of changes to the photo library, use the register(_:) method to designate an observer object. Whenever you use a fetch method, such as fetchAssets(with:), to retrieve assets or collections, Photos automatically registers your interest in observing changes to those items. After you perform a fetch, Photos sends messages to your observer through the PHPhotoLibraryChangeObserver protocol whenever the items in the resulting fetch request change. For example, you'll get notified when changes add items, remove items, or reorder the list of items in the fetch result. Update your user interface based on the details you receive from PHChange objects.

Use the shared PHPhotoLibrary object to register a change handler for the assets and collections you fetch. Photos tells your app whenever another app or device changes the content or metadata of an asset or the list of assets in a collection. PHChange objects provide information about object state before and after each change with semantics that make it easy to update a collection view or similar interface.

For information about handling changes, see the PHPhotoLibraryChangeObserver protocol.

See Also

Observing 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.

class PHFetchResultChangeDetails

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