Generic Class

PHFetchResult

An ordered list of assets or collections returned from a Photos fetch method.

Overview

When you use class methods on the PHAsset, PHCollection, PHAssetCollection, and PHCollectionList classes to retrieve objects, Photos provides the resulting objects in a fetch result. You access the contents of a fetch result with the same methods and conventions used by the NSArray class. Unlike an NSArray object, however, a PHFetchResult object dynamically loads its contents from the Photos library as needed, providing optimal performance even when handling a large number of results.

A fetch result provides thread-safe access to its contents. After a fetch, the fetch result’s count value is constant, and all objects in the fetch result keep the same localIdentifier value. (To get updated content for a fetch, register a change observer with the shared PHPhotoLibrary object.)

A fetch result caches its contents, keeping a batch of objects around the most recently accessed index. Because objects outside of the batch are no longer cached, accessing these objects results in refetching those objects. This process can result in changes to values previously read from those objects.

Topics

Querying a Fetch Result

func contains(ObjectType)

Returns whether the specified object is present in the fetch result.

var count: Int

The number of objects in the fetch result.

func countOfAssets(with: PHAssetMediaType)

Returns the number of assets in the fetch result of a specified type.

var firstObject: ObjectType?

The first object in the fetch result.

var lastObject: ObjectType?

The last object in the fetch result.

func object(at: Int)

Returns the object located at the specified index.

subscript(Int)

Returns the object located at the specified index.

func objects(at: IndexSet)

Returns an array containing the objects in the fetch result at the indexes in the specified index set.

Finding Objects in a Fetch Result

func index(of: ObjectType)

Returns the lowest index whose corresponding object in the fetch result is equal to the specified object.

func index(of: ObjectType, in: NSRange)

Returns the lowest index within the specified range whose corresponding object in the fetch result is equal to the specified object.

Performing Operations with Objects in a Fetch Result

func enumerateObjects(at: IndexSet, options: NSEnumerationOptions = [], using: (ObjectType, Int, UnsafeMutablePointer<ObjCBool>) -> Void)

Executes the specified block using the objects in the fetch result at the specified indexes.

func enumerateObjects((ObjectType, Int, UnsafeMutablePointer<ObjCBool>) -> Void)

Executes the specified block using each object in the fetch result, starting with the first object and continuing in order to the last object.

Relationships

Generic Constraints

  • ObjectType : AnyObject
    

Inherits From

See Also

Retrieving and Examining Assets

class PHAsset

A representation of an image, video or Live Photo in the Photos library.

class PHAssetCollection

A representation of a Photos asset grouping, such as a moment, user-created album, or smart album.

class PHCollectionList

A representation of a group containing Photos asset collections, such as a moments year or a folder of user-created albums.

class PHCollection

The abstract superclass for Photos asset collections and collection lists.

class PHObject

The abstract superclass for Photos model objects (assets and collections).

class PHFetchOptions

A set of options that affect the filtering, sorting, and management of results that Photos returns when you fetch asset or collection objects.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software