Skip to content

Commit 48d9afa

Browse files
Adds getting stated section to readme
1 parent f202c22 commit 48d9afa

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# ProgrammaticCoreData
22

3+
ProgrammaticCoreData is a library for iOS and macOS for creating CoreData's `NSManagedObjectModel`s programmatically.
4+
5+
### Why might I want create my Core Data models programmatically (and with ProgrammaticCoreData):
6+
- Shipping core data models in packages is a pain.
7+
- When [linking multiple core data stores](https://developer.apple.com/documentation/coredata/linking_data_between_two_core_data_stores) certain CoreData features are limmited or unavailable. Relationships across data stores for example are not supported (fetched properties can be used instead but have to be updated manually). Programmatic data models can be combined, resulting in one store and the ability to use relationships.
8+
- When [removing optionals from your CoreData code](https://www.jessesquires.com/blog/2022/01/26/core-data-optionals/) propperty types and their optionality in the Xcode data model and the manually created `Entities` have to match. When changing a propperty in either place the other has to be updated manually, which introduces a place for failure.
9+
- Xcode's CoreData models are stored as XML files. Reviewing changes can be difficult. A programmatic CoreData model makes reviews simpler
10+
- Data models created with ProgrammaticCoreData are declarative.
11+
12+
### Reasons for not creating your data models programmatically:
13+
- Migrations. A [humorous writeup](https://medium.com/@JohnCoatesDev/dont-create-core-data-models-programmatically-3a563e66ce2a) chronicles the pains of how to get migrations to work with programmatically created data models. There is verry little public documentation for migrating CoreData models that were not created with Xcode's CoreData model editor. [It is possible though](https://github.com/JohnCoates/Slate/tree/master/Source/Database).
14+
315
## Author
416

517
Axel Ancona Esselmann, axel@anconaesselmann.com
@@ -20,4 +32,44 @@ or add the following dependency to your `Package.swift`:
2032

2133
```swift
2234
.package(url: "https://github.com/anconaesselmann/ProgrammaticCoreData.git", from: "0.0.3")
23-
```
35+
```
36+
37+
38+
## Getting started
39+
40+
The example projects are a good starting point
41+
42+
- [Example_01](https://github.com/anconaesselmann/ProgrammaticCoreData/tree/main/Examples/Example_01) is a simple notes application without any relationships between entities:
43+
44+
We create out `Note` entity programmatically:
45+
```swift
46+
@objc(Note)
47+
public class Note: NSManagedObject, Identifiable {
48+
@nonobjc public class func fetchRequest() -> NSFetchRequest<Note> {
49+
return NSFetchRequest<Note>(entityName: "Note")
50+
}
51+
52+
@NSManaged public var id: UUID
53+
@NSManaged public var text: String
54+
@NSManaged public var created: Date
55+
}
56+
```
57+
58+
Extending `Note` to conform to `SelfDescribingCoreDataEntity` will give us a declarative representation of `Note`'s entity description:
59+
```swift
60+
extension Note: SelfDescribingCoreDataEntity {
61+
public static var entityDescription = Note.description(
62+
.uuid(\.id),
63+
.string(\.text),
64+
.date(\.created)
65+
)
66+
}
67+
```
68+
69+
We can now build our data model and create a container:
70+
```swift
71+
let container = try await NSManagedObjectModel(
72+
Note.self
73+
)
74+
.createContainer(name: "Notes", location: .local)
75+
```

0 commit comments

Comments
 (0)