Skip to content

Commit c1015cb

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

5 files changed

Lines changed: 25 additions & 34 deletions

File tree

Examples/Example_01/Example_01/Persistence/CoreDataModels/NoteEntity.swift

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

88
@objc(NoteEntity)
9-
public class NoteEntity: NSManagedObject, Identifiable {
10-
@nonobjc public class func fetchRequest() -> NSFetchRequest<NoteEntity> {
11-
return NSFetchRequest<NoteEntity>(entityName: "NoteEntity")
12-
}
9+
final class NoteEntity: NSManagedObject, Identifiable {
1310

14-
@NSManaged public var id: UUID
15-
@NSManaged public var text: String
16-
@NSManaged public var created: Date
11+
@NSManaged var id: UUID
12+
@NSManaged var text: String
13+
@NSManaged var created: Date
1714

18-
public init(
15+
// The only available initializer initializes all none-optional properties.
16+
// Below we mark all other initializers as unavailable. The compiler can now
17+
// enforce that we never instantiate an instance without setting all propperties.
18+
init(
1919
context: NSManagedObjectContext,
2020
id: UUID,
2121
text: String,
@@ -27,24 +27,25 @@ public class NoteEntity: NSManagedObject, Identifiable {
2727
self.created = created
2828
}
2929

30+
// MARK: - Unavailable initializers
3031
@available(*, unavailable)
31-
public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
32+
override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
3233
super.init(entity: entity, insertInto: context)
3334
}
3435

3536
@available(*, unavailable)
36-
public init(context: NSManagedObjectContext) {
37+
init(context: NSManagedObjectContext) {
3738
fatalError()
3839
}
3940

4041
@available(*, unavailable)
41-
public init() {
42+
init() {
4243
fatalError()
4344
}
4445
}
4546

4647
extension NoteEntity: SelfDescribingCoreDataEntity {
47-
public static var entityDescription = NoteEntity.description(
48+
static var entityDescription = NoteEntity.description(
4849
.uuid(\.id),
4950
.string(\.text),
5051
.date(\.created)

Examples/Example_02/Example_02.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@
140140
97AB88B72BE97DE100AB314B /* Persistence */ = {
141141
isa = PBXGroup;
142142
children = (
143-
97AB88B82BE97EEA00AB314B /* Models */,
144143
97AB88C52BE984A100AB314B /* ContainerBuilder.swift */,
144+
97AB88B82BE97EEA00AB314B /* Models */,
145145
);
146146
path = Persistence;
147147
sourceTree = "<group>";

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import ProgrammaticCoreData
77

88
@objc(AuthorEntity)
99
final class AuthorEntity: NSManagedObject, Identifiable {
10-
@nonobjc class func fetchRequest() -> NSFetchRequest<AuthorEntity> {
11-
return NSFetchRequest<AuthorEntity>(entityName: "AuthorEntity")
12-
}
1310

1411
@NSManaged var id: UUID
1512
@NSManaged var name: String
@@ -31,6 +28,9 @@ final class AuthorEntity: NSManagedObject, Identifiable {
3128
@objc(remove_books:)
3229
@NSManaged func removeFromBooks(_ values: Set<BookEntity>)
3330

31+
// The only available initializer initializes all none-optional properties.
32+
// Below we mark all other initializers as unavailable. The compiler can now
33+
// enforce that we never instantiate an instance without setting all propperties.
3434
init(
3535
context: NSManagedObjectContext,
3636
id: UUID,
@@ -41,6 +41,7 @@ final class AuthorEntity: NSManagedObject, Identifiable {
4141
self.name = name
4242
}
4343

44+
// MARK: - Unavailable initializers
4445
@available(*, unavailable)
4546
override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
4647
super.init(entity: entity, insertInto: context)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import ProgrammaticCoreData
77

88
@objc(BookEntity)
99
final class BookEntity: NSManagedObject, Identifiable {
10-
@nonobjc class func fetchRequest() -> NSFetchRequest<BookEntity> {
11-
return NSFetchRequest<BookEntity>(entityName: "BookEntity")
12-
}
1310

1411
@NSManaged var id: UUID
1512
@NSManaged var title: String
1613
@NSManaged var author: AuthorEntity
1714

15+
// The only available initializer initializes all none-optional properties.
16+
// Below we mark all other initializers as unavailable. The compiler can now
17+
// enforce that we never instantiate an instance without setting all propperties.
1818
init(
1919
context: NSManagedObjectContext,
2020
id: UUID,
@@ -28,6 +28,7 @@ final class BookEntity: NSManagedObject, Identifiable {
2828
author.addToBooks(self)
2929
}
3030

31+
// MARK: - Unavailable initializers
3132
@available(*, unavailable)
3233
override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
3334
super.init(entity: entity, insertInto: context)

README.md

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ We create our `Note` entity programmatically:
4646
```swift
4747
@objc(Note)
4848
final class Note: NSManagedObject, Identifiable {
49-
@nonobjc class func fetchRequest() -> NSFetchRequest<Note> {
50-
return NSFetchRequest<Note>(entityName: "Note")
51-
}
5249

5350
@NSManaged var id: UUID
5451
@NSManaged var text: String
@@ -69,10 +66,8 @@ extension Note: SelfDescribingCoreDataEntity {
6966

7067
We can now build our data model and create a container:
7168
```swift
72-
let container = try await NSManagedObjectModel(
73-
Note.self
74-
)
75-
.createContainer(name: "Notes", location: .local)
69+
let container = try await NSManagedObjectModel(Note.self)
70+
.createContainer(name: "Notes", location: .local)
7671
```
7772

7873
- [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`:
@@ -81,9 +76,6 @@ We create an `Author` entity programmatically. Note the `addToBooks` and `remove
8176
```swift
8277
@objc(Author)
8378
final class Author: NSManagedObject, Identifiable {
84-
@nonobjc class func fetchRequest() -> NSFetchRequest<Author> {
85-
return NSFetchRequest<Author>(entityName: "Author")
86-
}
8779

8880
@NSManaged var id: UUID
8981
@NSManaged var name: String
@@ -111,9 +103,6 @@ We create a `Book` entity programmatically. Note that it should be impossible to
111103
```swift
112104
@objc(Book)
113105
final class Book: NSManagedObject, Identifiable {
114-
@nonobjc class func fetchRequest() -> NSFetchRequest<Book> {
115-
return NSFetchRequest<Book>(entityName: "Book")
116-
}
117106

118107
@NSManaged var id: UUID
119108
@NSManaged var title: String
@@ -184,8 +173,7 @@ We can now build our data model and create a container:
184173
let container = try await NSManagedObjectModel(
185174
Author.self,
186175
Book.self
187-
)
188-
.createContainer(name: "Books", location: .local)
176+
).createContainer(name: "Books", location: .local)
189177
```
190178

191179
If we didn't care about the order of books on our `Author` entity we would declare a `books` `Set` instead of an `Array`:

0 commit comments

Comments
 (0)