<!--
{
  "availability" : [
    "iOS: 3.0.0 -",
    "iPadOS: 3.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.4.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreData",
  "identifier" : "/documentation/CoreData/NSManagedObjectModel",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Core Data"
    ],
    "preciseIdentifier" : "c:objc(cs)NSManagedObjectModel"
  },
  "title" : "NSManagedObjectModel"
}
-->

# NSManagedObjectModel

A programmatic representation of the `.xcdatamodeld` file describing your objects.

```
class NSManagedObjectModel
```

## Overview

The model contains one or more `NSEntityDescription` objects representing the entities in the schema. Each `NSEntityDescription` object has property description objects (instances of subclasses of [`NSPropertyDescription`](/documentation/CoreData/NSPropertyDescription)) that represent the properties (or fields) of the entity in the schema. The Core Data framework uses this description in several ways:

- Constraining UI creation in Interface Builder
- Validating attribute and relationship values at runtime
- Mapping between your managed objects and a database or file-based schema for object persistence

A managed object model maintains a mapping between each of its entity objects and a corresponding managed object class for use with the persistent storage mechanisms in the Core Data framework. You can determine the entity for a particular managed object with the `entity` method.

You typically create managed object models using the data modeling tool in Xcode, but it’s possible to build a model programmatically if needed.

### Loading a model file

Managed object model files are typically stored in a project or a framework. To load a model, you provide an URL to the constructor. Note that loading a model doesn’t have the effect of loading all of its entities.

### Storing fetch requests

Frequently, you need a collection of objects that share features in common. Sometimes you can define those features (property values) in advance; sometimes you need to be able to supply values at runtime. For example, suppose you want to retrieve all movies owned by Pixar, or retrieve all movies that earned more than an amount specified by the user at runtime.

Fetch requests are often predefined in a managed object model as templates. They allow you to predefine named queries and their parameters in the model. Typically they contain variables that need to be substituted at runtime. `NSManagedObjectModel` provides an API to retrieve a stored fetch request by name, and to perform variable substitution—see [`fetchRequestTemplate(forName:)`](/documentation/CoreData/NSManagedObjectModel/fetchRequestTemplate(forName:)) and [`fetchRequestFromTemplate(withName:substitutionVariables:)`](/documentation/CoreData/NSManagedObjectModel/fetchRequestFromTemplate(withName:substitutionVariables:)).

You typically define fetch request templates using the Data Model editor in Xcode. You can also create fetch request templates programmatically, and associate them with a model using [`setFetchRequestTemplate(_:forName:)`](/documentation/CoreData/NSManagedObjectModel/setFetchRequestTemplate(_:forName:)).

### Supporting multiple configurations for the same model

You may want to specify different sets of entities for the same model to be used in different situations. For example, suppose certain entities should only be available if a user has administrative privileges. To support this requirement, a model may have more than one configuration. Each configuration is named, and has an associated set of entities. The sets may overlap. You establish configurations programmatically using [`setEntities(_:forConfigurationName:)`](/documentation/CoreData/NSManagedObjectModel/setEntities(_:forConfigurationName:)) or using the Xcode design tool, and retrieve the entities for a given configuration name using [`entities(forConfigurationName:)`](/documentation/CoreData/NSManagedObjectModel/entities(forConfigurationName:)).

### Changing models

Because a model describes the structure of the data in a persistent store, changing any parts of a model that alters the schema renders it incompatible with (and so unable to open) the stores it previously created. If you change your schema, you therefore need to migrate the data in existing stores to new version (see [Core Data Model Versioning and Data Migration Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/Introduction.html#//apple_ref/doc/uid/TP40004399)). For example, if you add a new entity or a new attribute to an existing entity, you *can’t* open old stores; if you add a validation constraint or set a new default value for an attribute, you *can* open old stores.

### Editing models at runtime

Managed object models are editable until they are used by an object graph manager (a managed object context or a persistent store coordinator). This allows you to create or modify them dynamically until their first use. However, once a model is being used, it *must not* be changed. This is enforced at runtime—when the object manager first fetches data using a model, the whole of that model becomes uneditable. Any attempt to mutate a model or any of its sub-objects after that point throws an exception. If you need to modify a model that’s in use, create a copy, modify the copy, and then discard the objects with the old model.

### Enumerating entities with fast enumeration

In macOS 10.5 and later and on iOS, `NSManagedObjectModel` supports the <doc://com.apple.documentation/documentation/Foundation/NSFastEnumeration> protocol. You can use this to enumerate over a model’s entities, as illustrated in the following example:

```objc
NSManagedObjectModel *aModel = ...;
for (NSEntityDescription *entity in aModel) {
    // entity is each instance of NSEntityDescription in aModel in turn
}
```

## Topics

### Creating a managed object model

[`-  initWithContentsOfURL:`](/documentation/CoreData/NSManagedObjectModel/init(contentsOf:))

Initializes the managed object model using the model file at the specified URL.

[`-  init`](/documentation/CoreData/NSManagedObjectModel/init())

Initializes an empty managed object model.

[`+  mergedModelFromBundles:`](/documentation/CoreData/NSManagedObjectModel/mergedModel(from:))

Returns a model created by merging all the models found in given bundles.

[`+  mergedModelFromBundles:forStoreMetadata:`](/documentation/CoreData/NSManagedObjectModel/mergedModel(from:forStoreMetadata:))

Returns a merged model from a specified array for the version information in provided metadata.

[`+  modelByMergingModels:`](/documentation/CoreData/NSManagedObjectModel/init(byMerging:))

Creates a single model from an array of existing models.

[`+  modelByMergingModels:forStoreMetadata:`](/documentation/CoreData/NSManagedObjectModel/init(byMerging:forStoreMetadata:))

Returns, for the version information in given metadata, a model merged from a given array of models.

### Managing entities and configurations

[`entities`](/documentation/CoreData/NSManagedObjectModel/entities)

The entities in the model.

[`entitiesByName`](/documentation/CoreData/NSManagedObjectModel/entitiesByName)

The entities of the model, keyed by name.

[`configurations`](/documentation/CoreData/NSManagedObjectModel/configurations)

All the available configuration names of the model.

[`-  entitiesForConfiguration:`](/documentation/CoreData/NSManagedObjectModel/entities(forConfigurationName:))

Returns the entities of the model for a specified configuration.

[`-  setEntities:forConfiguration:`](/documentation/CoreData/NSManagedObjectModel/setEntities(_:forConfigurationName:))

Associates the specified entities with the model using the given configuration name.

### Manipulating fetch request templates

[`fetchRequestTemplatesByName`](/documentation/CoreData/NSManagedObjectModel/fetchRequestTemplatesByName)

A dictionary of the receiver’s fetch request templates, keyed by name.

[`-  fetchRequestTemplateForName:`](/documentation/CoreData/NSManagedObjectModel/fetchRequestTemplate(forName:))

Returns the fetch request with a specified name.

[`-  fetchRequestFromTemplateWithName:substitutionVariables:`](/documentation/CoreData/NSManagedObjectModel/fetchRequestFromTemplate(withName:substitutionVariables:))

Returns a copy of the fetch request template with the variables substituted by values from the substitutions dictionary.

[`-  setFetchRequestTemplate:forName:`](/documentation/CoreData/NSManagedObjectModel/setFetchRequestTemplate(_:forName:))

Associates the specified fetch request with the receiver using the given name.

### Handling localization

[`localizationDictionary`](/documentation/CoreData/NSManagedObjectModel/localizationDictionary)

The localization dictionary of the model.

### Versioning and migrating entities

[`versionChecksum`](/documentation/CoreData/NSManagedObjectModel/versionChecksum)

The Base64-encoded 128-bit model version hash.

[`versionIdentifiers`](/documentation/CoreData/NSManagedObjectModel/versionIdentifiers)

The set of developer-defined version identifiers for the object model.

[`entityVersionHashesByName`](/documentation/CoreData/NSManagedObjectModel/entityVersionHashesByName)

The dictionary of the model’s entity names and their corresponding version hashes.

[`-  isConfiguration:compatibleWithStoreMetadata:`](/documentation/CoreData/NSManagedObjectModel/isConfiguration(withName:compatibleWithStoreMetadata:))

Returns a Boolean value that indicates whether a given configuration in the model is compatible with given metadata from a persistent store.

### Working with indexes

[`NSFetchIndexElementType`](/documentation/CoreData/NSFetchIndexElementType)

Defines the possible types of index elements.

[`NSFetchIndexDescription`](/documentation/CoreData/NSFetchIndexDescription)

The description of the index.

[`NSFetchIndexElementDescription`](/documentation/CoreData/NSFetchIndexElementDescription)

Description of an Index Element

## Relationships

### Conforms To

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSFastEnumeration`](/documentation/Foundation/NSFastEnumeration)

[`Hashable`](/documentation/Swift/Hashable)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Equatable`](/documentation/Swift/Equatable)

[`NSCopying`](/documentation/Foundation/NSCopying)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)