CoreDataModel

public struct CoreDataModel
extension CoreDataModel: Equatable

An instance of CoreDataModel represents a Core Data model — a .xcdatamodeld file package. It provides the model and store URLs as well as methods for interacting with the store.

Properties

  • The name of the Core Data model resource.

    Declaration

    Swift

    public let name: String
  • The bundle in which the model is located. The default is the main bundle.

    Declaration

    Swift

    public let bundle: Bundle
  • The type of the Core Data persistent store for the model. The default is .sqlite with the user’s documents directory.

    Declaration

    Swift

    public let storeType: StoreType
  • The file URL specifying the full path to the store.

    Note

    If the store is in-memory, then this value will be nil.

    Declaration

    Swift

    public var storeURL: URL? { get }
  • The file URL specifying the model file in the bundle specified by bundle.

    Declaration

    Swift

    public var modelURL: URL { get }
  • The database file name for the store.

    Declaration

    Swift

    public var databaseFileName: String { get }
  • The managed object model for the model specified by name.

    Declaration

    Swift

    public var managedObjectModel: NSManagedObjectModel { get }
  • Queries the meta data for the persistent store specified by the receiver and returns whether or not a migration is needed.

    Declaration

    Swift

    public var needsMigration: Bool { get }

    Return Value

    true if the store requires a migration, false otherwise.

Initialization

  • Constructs a new CoreDataModel instance with the specified name and bundle.

    Declaration

    Swift

    public init(name: String, bundle: Bundle = .main, storeType: StoreType = .sqlite(defaultDirectoryURL()))

    Parameters

    name

    The name of the Core Data model.

    bundle

    The bundle in which the model is located. The default is the main bundle.

    storeType

    The store type for the Core Data model. The default is .sqlite with the user’s documents directory.

    Return Value

    A new CoreDataModel instance.

Methods

  • The default directory used to initialize a CoreDataModel. On tvOS, this is the caches directory. All other platforms use the document directory.

    Declaration

    Swift

    public static func defaultDirectoryURL() -> URL
  • Removes the existing model store specfied by the receiver.

    Throws

    If removing the store fails or errors, then this function throws an NSError.

    Declaration

    Swift

    public func removeExistingStore() throws
  • Progressively migrates the persistent store of the CoreDataModel based on mapping models found in the model’s bundle. If the model returns false from needsMigration, then this function does nothing.

    Throws

    If an error occurs, either an NSError or a MigrationError is thrown. If an NSError is thrown, it could specify any of the following: an error checking persistent store metadata, an error from NSMigrationManager, or an error from NSFileManager.

    Warning

    Migration is only supported for on-disk persistent stores. A complete ‘path’ of mapping models must exist between the peristent store’s version and the model’s version.

    Declaration

    Swift

    public func migrate() throws