Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import PackageDescription

let package = Package(
name: "CoreDataModelDescription",
platforms: [.macOS(.v10_13),
.iOS(.v11),
.tvOS(.v11),
.watchOS(.v4)],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import CoreData


/// Describes and creates `NSEntityDescription`
@available(iOS 11.0, tvOS 11.0, macOS 10.13, *)
@available(iOS 11.0, tvOS 11.0, macOS 10.13, watchOS 4.0, *)
public struct CoreDataEntityDescription {

public static func entity(name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import CoreData


/// Describes `NSFetchIndexDescription`
@available(iOS 11.0, tvOS 11.0, macOS 10.13, *)
@available(iOS 11.0, tvOS 11.0, macOS 10.13, watchOS 4.0, *)
public struct CoreDataFetchIndexDescription {

/// Describes `NSFetchIndexElementDescription`
Expand Down
18 changes: 11 additions & 7 deletions Sources/CoreDataModelDescription/CoreDataModelDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import CoreData


/// Used to create `NSManagedObjectModel`
@available(iOS 11.0, tvOS 11.0, macOS 10.13, *)
@available(iOS 11.0, tvOS 11.0, macOS 10.13, watchOS 4.0, *)
public struct CoreDataModelDescription {

public var entities: [CoreDataEntityDescription]
Expand All @@ -20,10 +20,16 @@ public struct CoreDataModelDescription {
}

public func makeModel() -> NSManagedObjectModel {
let model = NSManagedObjectModel()
return NSManagedObjectModel(modelDescription: self)
}
}

public extension NSManagedObjectModel {
convenience init(modelDescription: CoreDataModelDescription) {
self.init()

// For convenience: the package objects use "Description" suffix, Core Data objects have no suffix.
let entitiesDescriptions = self.entities
let entitiesDescriptions = modelDescription.entities
let entities: [NSEntityDescription]

// Model creation has next steps:
Expand Down Expand Up @@ -153,12 +159,10 @@ public struct CoreDataModelDescription {

// Set entities and configurations

model.entities = entities
self.entities = entities

for (configurationName, entities) in configurationNameToEntities {
model.setEntities(entities, forConfigurationName: configurationName)
self.setEntities(entities, forConfigurationName: configurationName)
}

return model
}
}