Skip to content

Commit f9f8dc4

Browse files
Readme update
1 parent 947daf3 commit f9f8dc4

1 file changed

Lines changed: 42 additions & 32 deletions

File tree

README.md

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,47 @@ final class Author: NSManagedObject, Identifiable {
9797
}
9898
```
9999

100-
We create a `Book` entity programmatically. Note that it should be impossible to create a book without adding the book to it's authors `books` property, since we have an inverse relationship:
100+
We create a `Book` entity programmatically:
101+
```swift
102+
@objc(Book)
103+
final class Book: NSManagedObject, Identifiable {
104+
@NSManaged var id: UUID
105+
@NSManaged var title: String
106+
@NSManaged var author: Author
107+
}
108+
```
109+
110+
Extending `Author` to conform to `SelfDescribingCoreDataEntity` will give us a declarative representation of `Author`'s entity description. We also set up the to-many relationship with the `Book` entity:
111+
```swift
112+
extension Author: SelfDescribingCoreDataEntity {
113+
static var entityDescription = Author.description(
114+
.uuid(\.id),
115+
.string(\.name),
116+
.relationship(\._books, .init(
117+
inverse: \Book.author,
118+
deleteRule: .cascadeDeleteRule,
119+
relationshipType: .toMany(isOrdered: true)
120+
))
121+
)
122+
}
123+
```
124+
125+
Extending `Book` to conform to `SelfDescribingCoreDataEntity` will give us a declarative representation of `Book`'s entity description. We also set up the to-one relationship to the `Book`'s `Author` entity:
126+
```swift
127+
extension Book: SelfDescribingCoreDataEntity {
128+
static var entityDescription = Book.description(
129+
.uuid(\.id),
130+
.string(\.title),
131+
.relationship(\.author, .init(
132+
inverse: \Author._books,
133+
deleteRule: .nullifyDeleteRule,
134+
relationshipType: .toOne
135+
))
136+
)
137+
}
138+
```
139+
140+
Note that it should be impossible to create a book without adding the book to it's authors `books` property, since we have an inverse relationship. We can achieve this by marking all but our own `Book` initializer as unavailable:
101141
```swift
102142
@objc(Book)
103143
final class Book: NSManagedObject, Identifiable {
@@ -108,7 +148,7 @@ final class Book: NSManagedObject, Identifiable {
108148

109149
// The only available initializer initializes all none-optional properties.
110150
// Below we mark all other initializers as unavailable. The compiler can now
111-
// enforce that we never instantiate an instance without setting all propperties.
151+
// enforce that we never instantiate an instance without setting all properties.
112152
init(
113153
context: NSManagedObjectContext,
114154
id: UUID,
@@ -140,36 +180,6 @@ final class Book: NSManagedObject, Identifiable {
140180
}
141181
```
142182

143-
Extending `Author` to conform to `SelfDescribingCoreDataEntity` will give us a declarative representation of `Author`'s entity description. We also set up the to-many relationship to the `Book` entity:
144-
```swift
145-
extension Author: SelfDescribingCoreDataEntity {
146-
static var entityDescription = Author.description(
147-
.uuid(\.id),
148-
.string(\.name),
149-
.relationship(\._books, .init(
150-
inverse: \Book.author,
151-
deleteRule: .cascadeDeleteRule,
152-
relationshipType: .toMany(isOrdered: true)
153-
))
154-
)
155-
}
156-
```
157-
158-
Extending `Book` to conform to `SelfDescribingCoreDataEntity` will give us a declarative representation of `Book`'s entity description. We also set up the to-one relationship to the `Book`'s `Author` entity:
159-
```swift
160-
extension Book: SelfDescribingCoreDataEntity {
161-
static var entityDescription = Book.description(
162-
.uuid(\.id),
163-
.string(\.title),
164-
.relationship(\.author, .init(
165-
inverse: \Author._books,
166-
deleteRule: .nullifyDeleteRule,
167-
relationshipType: .toOne
168-
))
169-
)
170-
}
171-
```
172-
173183
We can now build our data model and create a container:
174184
```swift
175185
let container = try await NSManagedObjectModel(

0 commit comments

Comments
 (0)