Skip to content

Commit 81db7aa

Browse files
Readme update and refactor of examples
1 parent c1015cb commit 81db7aa

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

Examples/Example_02/Example_02/Persistence/Models/AuthorEntity.swift

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,15 @@ final class AuthorEntity: NSManagedObject, Identifiable {
1212
@NSManaged var name: String
1313
@NSManaged var _books: NSOrderedSet
1414

15+
// CoreData requires an `NSSet` or `NSOrderedSet` for relationships.
16+
// (For ordered sets make sure to set 'isOrdered' = true in `entityDescription` below)
17+
// In this example we use `_` to "hide" the inner none-type-safe objective-c workings
18+
// and we only expose a read-only Swift array of `Book`s. We add to `_books` with
19+
// the auto-generated methods below.
1520
var books: Array<BookEntity> {
1621
get { _books.array as! Array<BookEntity> }
1722
}
1823

19-
@objc(add_booksObject:)
20-
@NSManaged func addToBooks(_ value: BookEntity)
21-
22-
@objc(remove_booksObject:)
23-
@NSManaged func removeFromBooks(_ value: BookEntity)
24-
25-
@objc(add_books:)
26-
@NSManaged func addToBooks(_ values: Set<BookEntity>)
27-
28-
@objc(remove_books:)
29-
@NSManaged func removeFromBooks(_ values: Set<BookEntity>)
30-
3124
// The only available initializer initializes all none-optional properties.
3225
// Below we mark all other initializers as unavailable. The compiler can now
3326
// enforce that we never instantiate an instance without setting all propperties.
@@ -56,6 +49,20 @@ final class AuthorEntity: NSManagedObject, Identifiable {
5649
init() {
5750
fatalError()
5851
}
52+
53+
// MARK: - Auto-generated methods for manipulating `_books`
54+
@objc(add_booksObject:)
55+
@NSManaged func addToBooks(_ value: BookEntity)
56+
57+
@objc(remove_booksObject:)
58+
@NSManaged func removeFromBooks(_ value: BookEntity)
59+
60+
// MARK: - Not used in this project but also auto-generated:
61+
@objc(add_books:)
62+
@NSManaged func addToBooks(_ values: Set<BookEntity>)
63+
64+
@objc(remove_books:)
65+
@NSManaged func removeFromBooks(_ values: Set<BookEntity>)
5966
}
6067

6168
extension AuthorEntity: SelfDescribingCoreDataEntity {

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ We create our `Note` entity programmatically:
4646
```swift
4747
@objc(Note)
4848
final class Note: NSManagedObject, Identifiable {
49-
5049
@NSManaged var id: UUID
5150
@NSManaged var text: String
5251
@NSManaged var created: Date
@@ -72,15 +71,18 @@ let container = try await NSManagedObjectModel(Note.self)
7271

7372
- [Example_02](https://github.com/anconaesselmann/ProgrammaticCoreData/tree/main/Examples/Example_02) is a Book Archive app with a to-many relationship from an `Author` to their `Book`s and an inverse to-one relationship from a `Book` and it's `Author`:
7473

75-
We create an `Author` entity programmatically. Note the `addToBooks` and `removeFromBooks` function signatures. We are exposing objective-c methods that get generated for us to add to our `_books` ordered set:
74+
We create an `Author` entity programmatically:
7675
```swift
7776
@objc(Author)
7877
final class Author: NSManagedObject, Identifiable {
79-
8078
@NSManaged var id: UUID
8179
@NSManaged var name: String
8280
@NSManaged var _books: NSOrderedSet
8381

82+
// CoreData requires an `NSSet` or `NSOrderedSet` for relationships. In this example we
83+
// use `_` to "hide" the inner none-type-safe objective-c workings and we only
84+
// expose a read-only Swift array of `Book`s. We add to `_books` with the auto-generated
85+
// methods below.
8486
var books: Array<Book> {
8587
get { _books.array as! Array<Book> }
8688
}
@@ -90,16 +92,10 @@ final class Author: NSManagedObject, Identifiable {
9092

9193
@objc(remove_booksObject:)
9294
@NSManaged func removeFromBooks(_ value: Book)
93-
94-
@objc(add_books:)
95-
@NSManaged func addToBooks(_ values: Set<Book>)
96-
97-
@objc(remove_books:)
98-
@NSManaged func removeFromBooks(_ values: Set<Book>)
9995
}
10096
```
10197

102-
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. We achieve this by creating an initializer that adds the book to it's `Author` and marking all other initializers as `unavailable`:
98+
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:
10399
```swift
104100
@objc(Book)
105101
final class Book: NSManagedObject, Identifiable {
@@ -108,6 +104,9 @@ final class Book: NSManagedObject, Identifiable {
108104
@NSManaged var title: String
109105
@NSManaged var author: Author
110106

107+
// The only available initializer initializes all none-optional properties.
108+
// Below we mark all other initializers as unavailable. The compiler can now
109+
// enforce that we never instantiate an instance without setting all propperties.
111110
init(
112111
context: NSManagedObjectContext,
113112
id: UUID,
@@ -121,6 +120,7 @@ final class Book: NSManagedObject, Identifiable {
121120
author.addToBooks(self)
122121
}
123122

123+
// MARK: - Unavailable initializers
124124
@available(*, unavailable)
125125
override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
126126
super.init(entity: entity, insertInto: context)

0 commit comments

Comments
 (0)