Skip to content
Declarative way to describe a Core Data model in code.
Swift
Branch: master
Clone or download
dmytro-anokhin Merge pull request #3 from vander2675/master
Relationship NSDelteRule was not getting set
Latest commit 47ea087 Aug 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Sources/CoreDataModelDescription Relationship NSDelteRule now actually gets set on NSRelationShipDescr… Aug 22, 2019
Tests Added Configurations Aug 19, 2019
.gitignore Update gitignore Aug 13, 2019
Package.swift Initial Commit Aug 12, 2019
README.md Update README.md Aug 15, 2019

README.md

CoreDataModelDescription

Declarative way to describe a Core Data model in code.

Usage

Use CoreDataModelDescription to describe your model. Sample code describes this model:

Image of Author, Publication, and Article model

Assuming you already defined Author, Publication, and Article subclasses of NSManagedObject.

let modelDescription = CoreDataModelDescription(
    entities: [
        .entity(
            name: "Author",
            managedObjectClass: Author.self,
            attributes: [
                .attribute(name: "name", type: .stringAttributeType)
            ],
            relationships: [
                .relationship(name: "publications", destination: "Publication", toMany: true, deleteRule: .cascadeDeleteRule, inverse: "author")
            ]),
        .entity(
            name: "Publication",
            managedObjectClass: Publication.self,
            attributes: [
                .attribute(name: "publicationDate", type: .dateAttributeType),
                .attribute(name: "numberOfViews", type: .integer64AttributeType, isOptional: true)
            ],
            relationships: [
                .relationship(name: "author", destination: "Author", toMany: false, inverse: "publications")
            ]),
        .entity(
            name: "Article",
            managedObjectClass: Article.self,
            parentEntity: "Publication",
            attributes: [
                .attribute(name: "text", type: .stringAttributeType)
            ])
    ]
)

let model = modelDescription.makeModel()

Motivation

Motivation and creating this package described in my article Core Data and Swift Package Manager.

You can’t perform that action at this time.