Contents

beginAccessingModelCollectionWithIdentifier:completionHandler:

Requests access to a model collection.

Declaration

+ (NSProgress *) beginAccessingModelCollectionWithIdentifier:(NSString *) identifier completionHandler:(void (^)(MLModelCollection *modelCollection, NSError *error)) completionHandler;

Parameters

  • identifier:

    The name of the model collection.

  • completionHandler:

    In Objective-C, the block that Core ML calls when the model collections is available to your app.

    The block has the following parameters:

    model

    An Mlmodelcollection instance if the collection loaded successfully.

    error

    An Nserror instance if the model collection failed to load (see Code).

Discussion

Use this method to access a model collection from a deployment (see Creating and Deploying a Model Collection). Your app can call this method multiple times, typically each time it needs to access the models in the collection.

Core ML calls your completion app’s completion handler once the entire collection is on the user’s device. If the model collection isn’t already on the user’s device, Core ML immediately begins fetching it over the network.

func modelCollectionAvailable(result: Result<MLModelCollection, Error>) {
    switch result {
    case .success(let collection):
        print("Model collection `\(collection.identifier)` is now available.")
        
        // Load a model from the collection.
        loadModel("DeploymentModel", from: collection)
        
    case .failure(let error):
        print("Error accessing a model collection: \(error)")
    }
}

Once your app has the model collection, use the model’s identifier that you assigned it in the deployment (see Creating and Deploying a Model Collection).

Create an instance of a collection’s model by passing its entry’s modelURL — from the model collection’s entries property — to the load(contentsOf:configuration:completionHandler:) class method.

If your app uses a default model from the app bundle, you can use an initializer from its convenience wrapper class that Xcode automatically generated.

let modelFromCollection = try? BuiltInModel(contentsOf: entry.modelURL)

See Also

Accessing a model collection