PHAssetCollectionChangeRequest

PHAssetCollectionChangeRequest

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

class PHAssetCollectionChangeRequest

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.

Swift:

PHPhotoLibrary.shared().performChanges {

    let request = PHAssetCollectionChangeRequest(for: myAlbum,
                                                 assets: albumAssetsFetchResult)
    
    request!.removeAssets([asset] as NSFastEnumeration)

} completionHandler: { success, error in
    print("Finished removing the asset from the album. \(success ? "Success" : String(describing: error))")
}

Objective-C:

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        
    PHAssetCollectionChangeRequest *request =
        [PHAssetCollectionChangeRequest changeRequestForAssetCollection:myAlbum
                                                                 assets:albumAssetsFetchResult];
        
    [request removeAssets:@[asset]];
     
} completionHandler:^(BOOL success, NSError *error) {
    NSLog(@"Finished removing the 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.

Inherits From

PHChangeRequest

Conforms To

CVarArg CustomDebugStringConvertible CustomStringConvertible Equatable Hashable NSObjectProtocol