Class

PHAssetCollectionChangeRequest

A request to create, delete, or modify a Photos asset collection, for use in a photo library change block.

Overview

You use the PHAssetCollectionChangeRequest class to request changes for PHAssetCollection objects. To make changes to asset collections (such as user-created albums) in the Photos library, create a change request using the appropriate class method for the change you want to perform.

Before creating a change request, use the canPerform(_:) method to verify that the collection allows the edit operation you’re requesting. If you attempt to perform an unsupported edit operation, Photos throws an exception.

A change request for creating or modifying an asset collection works like a mutable version of the asset collection object. Use the change request’s properties and instance methods to request changes to the asset collection itself. For example, the following code removes an asset from an album:

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest
        changeRequestForAssetCollection:self.myAlbum
                                 assets:self.albumAssetsFetchResult];
    [request removeAssets:@[asset]];
 
} completionHandler:^(BOOL success, NSError *error) {
    NSLog(@"Finished removing asset from the album. %@", (success ? @"Success" : error));
}];

After Photos runs the change block and calls your completion handler, the asset collection’s state reflects the changes you requested in the block.

If you create or use a change request object outside a photo library change block, Photos raises an Objective-C exception. For details on change blocks, see PHPhotoLibrary.

Topics

Adding New Asset Collections

class func creationRequestForAssetCollection(withTitle: String)

Creates a request for adding a new asset collection to the Photos library.

var placeholderForCreatedAssetCollection: PHObjectPlaceholder

A placeholder object for the asset collection that the change request creates.

Deleting Asset Collections

class func deleteAssetCollections(NSFastEnumeration)

Requests that the specified asset collections be deleted.

Modifying Asset Collections

init?(for: PHAssetCollection)

Creates a request for modifying the specified asset collection.

init?(for: PHAssetCollection, assets: PHFetchResult<PHAsset>)

Creates a request for modifying the specified asset collection, with a fetch result for tracking changes.

var title: String

The displayed name of the asset collection.

func addAssets(NSFastEnumeration)

Add the specified assets to the asset collection.

func insertAssets(NSFastEnumeration, at: IndexSet)

Inserts the specified assets into the collection at the specified indexes.

func removeAssets(NSFastEnumeration)

Removes the specified assets from the asset collection.

func removeAssets(at: IndexSet)

Removes the assets at the specified indexes from the asset collection.

func replaceAssets(at: IndexSet, withAssets: NSFastEnumeration)

Replaces the assets at the specified indexes in the asset collection with the specified assets.

func moveAssets(at: IndexSet, to: Int)

Moves the assets at the specified indexes in the asset collection to a new index.

Relationships

Inherits From

Conforms To

See Also

Requesting Changes

class PHAssetChangeRequest

A request to create, delete, change metadata for, or edit the content of a Photos asset, for use in a photo library change block.

class PHCollectionListChangeRequest

A request to create, delete, or modify a Photos collection list, for use in a photo library change block.

class PHObjectPlaceholder

A read-only proxy representing a Photos asset or collection object yet to be created by a change request.