Skip to content

Commit 26ecd49

Browse files
Readme update and refactor of examples
1 parent d6ef0f8 commit 26ecd49

3 files changed

Lines changed: 54 additions & 54 deletions

File tree

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,32 @@ import CoreData
66
import ProgrammaticCoreData
77

88
@objc(AuthorEntity)
9-
final public class AuthorEntity: NSManagedObject, Identifiable {
10-
@nonobjc public class func fetchRequest() -> NSFetchRequest<AuthorEntity> {
9+
final class AuthorEntity: NSManagedObject, Identifiable {
10+
@nonobjc class func fetchRequest() -> NSFetchRequest<AuthorEntity> {
1111
return NSFetchRequest<AuthorEntity>(entityName: "AuthorEntity")
1212
}
1313

14-
@NSManaged public var id: UUID
15-
@NSManaged public var name: String
16-
@NSManaged public var _books: NSOrderedSet
14+
@NSManaged var id: UUID
15+
@NSManaged var name: String
16+
@NSManaged var _books: NSOrderedSet
1717

1818
var books: Array<BookEntity> {
1919
get { _books.array as! Array<BookEntity> }
2020
}
2121

2222
@objc(add_booksObject:)
23-
@NSManaged public func addToBooks(_ value: BookEntity)
23+
@NSManaged func addToBooks(_ value: BookEntity)
2424

2525
@objc(remove_booksObject:)
26-
@NSManaged public func removeFromBooks(_ value: BookEntity)
26+
@NSManaged func removeFromBooks(_ value: BookEntity)
2727

2828
@objc(add_books:)
29-
@NSManaged public func addToBooks(_ values: Set<BookEntity>)
29+
@NSManaged func addToBooks(_ values: Set<BookEntity>)
3030

3131
@objc(remove_books:)
32-
@NSManaged public func removeFromBooks(_ values: Set<BookEntity>)
32+
@NSManaged func removeFromBooks(_ values: Set<BookEntity>)
3333

34-
public init(
34+
init(
3535
context: NSManagedObjectContext,
3636
id: UUID,
3737
name: String
@@ -42,23 +42,23 @@ final public class AuthorEntity: NSManagedObject, Identifiable {
4242
}
4343

4444
@available(*, unavailable)
45-
public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
45+
override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
4646
super.init(entity: entity, insertInto: context)
4747
}
4848

4949
@available(*, unavailable)
50-
public init(context: NSManagedObjectContext) {
50+
init(context: NSManagedObjectContext) {
5151
fatalError()
5252
}
5353

5454
@available(*, unavailable)
55-
public init() {
55+
init() {
5656
fatalError()
5757
}
5858
}
5959

6060
extension AuthorEntity: SelfDescribingCoreDataEntity {
61-
public static var entityDescription = AuthorEntity.description(
61+
static var entityDescription = AuthorEntity.description(
6262
.uuid(\.id),
6363
.string(\.name),
6464
.relationship(\._books, .init(

Examples/Example_02/Example_02/Persistence/Models/BookEntity.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import CoreData
66
import ProgrammaticCoreData
77

88
@objc(BookEntity)
9-
public class BookEntity: NSManagedObject, Identifiable {
10-
@nonobjc public class func fetchRequest() -> NSFetchRequest<BookEntity> {
9+
final class BookEntity: NSManagedObject, Identifiable {
10+
@nonobjc class func fetchRequest() -> NSFetchRequest<BookEntity> {
1111
return NSFetchRequest<BookEntity>(entityName: "BookEntity")
1212
}
1313

14-
@NSManaged public var id: UUID
15-
@NSManaged public var title: String
16-
@NSManaged public var author: AuthorEntity
14+
@NSManaged var id: UUID
15+
@NSManaged var title: String
16+
@NSManaged var author: AuthorEntity
1717

18-
public init(
18+
init(
1919
context: NSManagedObjectContext,
2020
id: UUID,
2121
title: String,
@@ -29,23 +29,23 @@ public class BookEntity: NSManagedObject, Identifiable {
2929
}
3030

3131
@available(*, unavailable)
32-
public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
32+
override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
3333
super.init(entity: entity, insertInto: context)
3434
}
3535

3636
@available(*, unavailable)
37-
public init(context: NSManagedObjectContext) {
37+
init(context: NSManagedObjectContext) {
3838
fatalError()
3939
}
4040

4141
@available(*, unavailable)
42-
public init() {
42+
init() {
4343
fatalError()
4444
}
4545
}
4646

4747
extension BookEntity: SelfDescribingCoreDataEntity {
48-
public static var entityDescription = BookEntity.description(
48+
static var entityDescription = BookEntity.description(
4949
.uuid(\.id),
5050
.string(\.title),
5151
.relationship(\.author, .init(

README.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,21 @@ The example projects are a good starting point
4545
We create our `Note` entity programmatically:
4646
```swift
4747
@objc(Note)
48-
public class Note: NSManagedObject, Identifiable {
49-
@nonobjc public class func fetchRequest() -> NSFetchRequest<Note> {
48+
final class Note: NSManagedObject, Identifiable {
49+
@nonobjc class func fetchRequest() -> NSFetchRequest<Note> {
5050
return NSFetchRequest<Note>(entityName: "Note")
5151
}
5252

53-
@NSManaged public var id: UUID
54-
@NSManaged public var text: String
55-
@NSManaged public var created: Date
53+
@NSManaged var id: UUID
54+
@NSManaged var text: String
55+
@NSManaged var created: Date
5656
}
5757
```
5858

5959
Extending `Note` to conform to `SelfDescribingCoreDataEntity` will give us a declarative representation of `Note`'s entity description:
6060
```swift
6161
extension Note: SelfDescribingCoreDataEntity {
62-
public static var entityDescription = Note.description(
62+
static var entityDescription = Note.description(
6363
.uuid(\.id),
6464
.string(\.text),
6565
.date(\.created)
@@ -80,46 +80,46 @@ let container = try await NSManagedObjectModel(
8080
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:
8181
```swift
8282
@objc(Author)
83-
final public class Author: NSManagedObject, Identifiable {
84-
@nonobjc public class func fetchRequest() -> NSFetchRequest<Author> {
83+
final class Author: NSManagedObject, Identifiable {
84+
@nonobjc class func fetchRequest() -> NSFetchRequest<Author> {
8585
return NSFetchRequest<Author>(entityName: "Author")
8686
}
8787

88-
@NSManaged public var id: UUID
89-
@NSManaged public var name: String
90-
@NSManaged public var _books: NSOrderedSet
88+
@NSManaged var id: UUID
89+
@NSManaged var name: String
90+
@NSManaged var _books: NSOrderedSet
9191

92-
public var books: Array<Book> {
92+
var books: Array<Book> {
9393
get { _books.array as! Array<Book> }
9494
}
9595

9696
@objc(add_booksObject:)
97-
@NSManaged public func addToBooks(_ value: Book)
97+
@NSManaged func addToBooks(_ value: Book)
9898

9999
@objc(remove_booksObject:)
100-
@NSManaged public func removeFromBooks(_ value: Book)
100+
@NSManaged func removeFromBooks(_ value: Book)
101101

102102
@objc(add_books:)
103-
@NSManaged public func addToBooks(_ values: Set<Book>)
103+
@NSManaged func addToBooks(_ values: Set<Book>)
104104

105105
@objc(remove_books:)
106-
@NSManaged public func removeFromBooks(_ values: Set<Book>)
106+
@NSManaged func removeFromBooks(_ values: Set<Book>)
107107
}
108108
```
109109

110110
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`:
111111
```swift
112112
@objc(Book)
113-
final public class Book: NSManagedObject, Identifiable {
114-
@nonobjc public class func fetchRequest() -> NSFetchRequest<Book> {
113+
final class Book: NSManagedObject, Identifiable {
114+
@nonobjc class func fetchRequest() -> NSFetchRequest<Book> {
115115
return NSFetchRequest<Book>(entityName: "Book")
116116
}
117117

118-
@NSManaged public var id: UUID
119-
@NSManaged public var title: String
120-
@NSManaged public var author: Author
118+
@NSManaged var id: UUID
119+
@NSManaged var title: String
120+
@NSManaged var author: Author
121121

122-
public init(
122+
init(
123123
context: NSManagedObjectContext,
124124
id: UUID,
125125
title: String,
@@ -133,17 +133,17 @@ final public class Book: NSManagedObject, Identifiable {
133133
}
134134

135135
@available(*, unavailable)
136-
public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
136+
override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
137137
super.init(entity: entity, insertInto: context)
138138
}
139139

140140
@available(*, unavailable)
141-
public init(context: NSManagedObjectContext) {
141+
init(context: NSManagedObjectContext) {
142142
fatalError()
143143
}
144144

145145
@available(*, unavailable)
146-
public init() {
146+
init() {
147147
fatalError()
148148
}
149149
}
@@ -152,7 +152,7 @@ final public class Book: NSManagedObject, Identifiable {
152152
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:
153153
```swift
154154
extension Author: SelfDescribingCoreDataEntity {
155-
public static var entityDescription = Author.description(
155+
static var entityDescription = Author.description(
156156
.uuid(\.id),
157157
.string(\.name),
158158
.relationship(\._books, .init(
@@ -167,7 +167,7 @@ extension Author: SelfDescribingCoreDataEntity {
167167
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:
168168
```swift
169169
extension Book: SelfDescribingCoreDataEntity {
170-
public static var entityDescription = Book.description(
170+
static var entityDescription = Book.description(
171171
.uuid(\.id),
172172
.string(\.title),
173173
.relationship(\.author, .init(
@@ -192,9 +192,9 @@ If we didn't care about the order of books on our `Author` entity we would decla
192192

193193
```Swift
194194
@objc(Author)
195-
final public class Author: NSManagedObject, Identifiable {
195+
final class Author: NSManagedObject, Identifiable {
196196
// ...
197-
@NSManaged public var _books: NSSet
197+
@NSManaged var _books: NSSet
198198

199199
var books: Set<Book> {
200200
get { _books as! Set<Book> }
@@ -205,7 +205,7 @@ final public class Author: NSManagedObject, Identifiable {
205205
In the `Author`'s extension to conform to `SelfDescribingCoreDataEntity` we would set `isOrdered` to `false` or simply write `.toMany`:
206206
```Swift
207207
extension Author: SelfDescribingCoreDataEntity {
208-
public static var entityDescription = Author.description(
208+
static var entityDescription = Author.description(
209209
// ...
210210
.relationship(\._books, .init(
211211
inverse: \Book.author,

0 commit comments

Comments
 (0)