Instance Method

loadValuesAsynchronouslyForKeys:completionHandler:

Tells the asset to load the values of all of the specified keys (property names) that are not already loaded.

Required.

Declaration

- (void)loadValuesAsynchronouslyForKeys:(NSArray<NSString *> *)keys completionHandler:(void (^)(void))handler;

Parameters

keys

An array of strings containing the required keys.

The keys are the property names of a class that adopts the protocol.

handler

The block to be invoked when loading succeeds, fails, or is cancelled.

Discussion

Regardless of the number of keys specified, the completion handler will be invoked exactly once per invocation of this method. This method will be called:

  • Synchronously if all specified keys have previously been loaded (a status of AVKeyValueStatusLoaded) or if an I/O error or other format-related error occurs immediately.

  • Asynchronously when the values of all of the specified keys become loaded, if a loading error occurs at a later stage of processing, or if cancelLoading is invoked on an AVAsset instance. The callback will be invoked on an arbitrary background queue. You should dispatch back to the main queue before performing any user interface-related operations.

The completion states of the keys you specify in keys are not necessarily the same—some may be loaded, and others may have failed. You must check the status of each key individually using the statusOfValueForKey:error: method.

You can call this method at any time, even after an asset has begun to load data for operations in progress or already completed. Whenever any module requires a certain collection of key-values, it can call this method and be assured of receiving its specified callback as soon as possible.

The following example shows how to use this method to load an asset’s playable key:

// Load the asset's "playable" key
[asset loadValuesAsynchronouslyForKeys:@[@"playable"] completionHandler:^{
    NSError *error = nil;
    AVKeyValueStatus status =
        [asset statusOfValueForKey:@"playable" error:&error];
    switch (status) {
        case AVKeyValueStatusLoaded:
            // Sucessfully loaded, continue processing
            break;
        case AVKeyValueStatusFailed:
            // Examine NSError pointer to determine failure
            break;
        case AVKeyValueStatusCancelled:
            // Loading cancelled
            break;
        default:
            // Handle all other cases
            break;
    }
}];

See Also

Loading Assets by Key

- statusOfValueForKey:error:

Returns a status indicating whether the value for a given key is immediately available without blocking the calling thread.

Required.