Instance Method

loadValuesAsynchronously(forKeys:completionHandler:)

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

Required.

Declaration

func loadValuesAsynchronously(forKeys keys: [String], completionHandler handler: (() -> Void)? = nil)

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 AVKeyValueStatus.loaded) 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 statusOfValue(forKey: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 isPlayable key:

// Load the asset's "playable" key
asset.loadValuesAsynchronously(forKeys: ["playable"]) {
    var error: NSError? = nil
    let status = asset.statusOfValue(forKey: "playable", error: &error)
    switch status {
    case .loaded:
    // Sucessfully loaded, continue processing
    case .failed:
    // Examine NSError pointer to determine failure
    case .cancelled:
    // Loading cancelled
    default:
        // Handle all other cases
    }
}

See Also

Loading Assets by Key

func statusOfValue(forKey: String, error: NSErrorPointer) -> AVKeyValueStatus

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

Required.