You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+42-32Lines changed: 42 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -97,7 +97,47 @@ final class Author: NSManagedObject, Identifiable {
97
97
}
98
98
```
99
99
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
+
finalclassBook: NSManagedObject, Identifiable {
104
+
@NSManagedvar id: UUID
105
+
@NSManagedvar title: String
106
+
@NSManagedvar 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
+
extensionAuthor: SelfDescribingCoreDataEntity {
113
+
staticvar 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
+
extensionBook: SelfDescribingCoreDataEntity {
128
+
staticvar 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:
101
141
```swift
102
142
@objc(Book)
103
143
finalclassBook: NSManagedObject, Identifiable {
@@ -108,7 +148,7 @@ final class Book: NSManagedObject, Identifiable {
108
148
109
149
// The only available initializer initializes all none-optional properties.
110
150
// 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.
112
152
init(
113
153
context: NSManagedObjectContext,
114
154
id: UUID,
@@ -140,36 +180,6 @@ final class Book: NSManagedObject, Identifiable {
140
180
}
141
181
```
142
182
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
-
extensionAuthor: SelfDescribingCoreDataEntity {
146
-
staticvar 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
-
extensionBook: SelfDescribingCoreDataEntity {
161
-
staticvar 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
-
173
183
We can now build our data model and create a container:
0 commit comments