Skip to content

Commit 947daf3

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

6 files changed

Lines changed: 57 additions & 69 deletions

File tree

Examples/Example_01/Example_01/Managers/NotesManager.swift

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,26 @@ class NotesManager {
2222
self.container = container
2323
}
2424

25-
func fetch() async throws -> [NoteEntity] {
26-
try await viewContext.perform {
27-
let request = NoteEntity.fetchRequest()
28-
return try self.viewContext.fetch(request)
29-
}
25+
func fetch() throws -> [NoteEntity] {
26+
let request = NoteEntity.fetchRequest()
27+
return try self.viewContext.fetch(request)
3028
}
3129

32-
func create(text: String) async throws {
33-
try await viewContext.perform {
34-
let entity = NoteEntity(
35-
context: self.viewContext,
36-
id: UUID(),
37-
text: text,
38-
created: .now
39-
)
40-
try self.viewContext.save()
41-
self._noteCreated.send(entity)
42-
}
30+
func create(text: String) throws {
31+
let entity = NoteEntity(
32+
context: self.viewContext,
33+
id: UUID(),
34+
text: text,
35+
created: .now
36+
)
37+
try self.viewContext.save()
38+
self._noteCreated.send(entity)
4339
}
4440

45-
func delete(notes: [NoteEntity]) async throws {
46-
try await viewContext.perform {
47-
notes.forEach {
48-
self.viewContext.delete($0)
49-
}
50-
try self.viewContext.save()
41+
func delete(notes: [NoteEntity]) throws {
42+
notes.forEach {
43+
self.viewContext.delete($0)
5144
}
45+
try self.viewContext.save()
5246
}
5347
}

Examples/Example_01/Example_01/UI/CreateNoteView.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ struct CreateNoteView: View {
1515
TextField("new note", text: $text)
1616
.textFieldStyle(.roundedBorder)
1717
Button("create") {
18-
Task {
19-
try await notesManager.create(text: text)
18+
do {
19+
try notesManager.create(text: text)
2020
text = ""
21+
} catch {
22+
assertionFailure(error.localizedDescription)
2123
}
2224
}
2325
.disabled(text.isEmpty)

Examples/Example_01/Example_01/UI/NotesView.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ class NotesViewModel: ObservableObject {
2424
.store(in: &bag)
2525
}
2626

27-
func fetch() async {
28-
notes = (try? await notesManager.fetch()) ?? []
27+
func fetch() {
28+
notes = (try? notesManager.fetch()) ?? []
2929
}
3030

3131
func delete(indexSet: IndexSet) {
3232
let toDelete = indexSet.map { notes[$0] }
33-
Task {
34-
try await notesManager.delete(notes: toDelete)
33+
do {
34+
try notesManager.delete(notes: toDelete)
3535
notes.remove(atOffsets: indexSet)
36+
} catch {
37+
assertionFailure(error.localizedDescription)
3638
}
3739
}
3840
}
@@ -55,7 +57,7 @@ struct NotesView: View {
5557
}
5658
}
5759
.task {
58-
await vm.fetch()
60+
vm.fetch()
5961
}
6062
}
6163
}

Examples/Example_02/Example_02/Managers/AuthorsManager.swift

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,25 @@ class AuthorsManager {
2222
self.container = container
2323
}
2424

25-
func fetch() async throws -> [AuthorEntity] {
26-
try await viewContext.perform {
27-
let request = AuthorEntity.fetchRequest()
28-
return try self.viewContext.fetch(request)
29-
}
25+
func fetch() throws -> [AuthorEntity] {
26+
let request = AuthorEntity.fetchRequest()
27+
return try self.viewContext.fetch(request)
3028
}
3129

32-
func create(name: String) async throws {
33-
try await viewContext.perform {
34-
let entity = AuthorEntity(
35-
context: self.viewContext,
36-
id: UUID(),
37-
name: name
38-
)
39-
try self.viewContext.save()
40-
self._authorCreated.send(entity)
41-
}
30+
func create(name: String) throws {
31+
let entity = AuthorEntity(
32+
context: self.viewContext,
33+
id: UUID(),
34+
name: name
35+
)
36+
try self.viewContext.save()
37+
self._authorCreated.send(entity)
4238
}
4339

44-
func delete(authors: [AuthorEntity]) async throws {
45-
try await viewContext.perform {
46-
authors.forEach {
47-
self.viewContext.delete($0)
48-
}
49-
try self.viewContext.save()
40+
func delete(authors: [AuthorEntity]) throws {
41+
authors.forEach {
42+
self.viewContext.delete($0)
5043
}
44+
try self.viewContext.save()
5145
}
5246
}

Examples/Example_02/Example_02/Managers/BooksManager.swift

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,25 @@ class BooksManager {
2323
}
2424

2525
func fetch() async throws -> [BookEntity] {
26-
try await viewContext.perform {
27-
let request = BookEntity.fetchRequest()
28-
return try self.viewContext.fetch(request)
29-
}
26+
let request = BookEntity.fetchRequest()
27+
return try self.viewContext.fetch(request)
3028
}
3129

3230
func create(title: String, author: AuthorEntity) async throws {
33-
try await viewContext.perform {
34-
let entity = BookEntity(
35-
context: self.viewContext,
36-
id: UUID(),
37-
title: title,
38-
author: author
39-
)
40-
try self.viewContext.save()
41-
self._bookCreated.send(entity)
42-
}
31+
let entity = BookEntity(
32+
context: self.viewContext,
33+
id: UUID(),
34+
title: title,
35+
author: author
36+
)
37+
try self.viewContext.save()
38+
self._bookCreated.send(entity)
4339
}
4440

4541
func delete(books: [BookEntity]) async throws {
46-
try await viewContext.perform {
47-
books.forEach {
48-
self.viewContext.delete($0)
49-
}
50-
try self.viewContext.save()
42+
books.forEach {
43+
self.viewContext.delete($0)
5144
}
45+
try self.viewContext.save()
5246
}
5347
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ let container = try await NSManagedObjectModel(Note.self)
6969
.createContainer(name: "Notes", location: .local)
7070
```
7171

72+
See [NotesManager](https://github.com/anconaesselmann/ProgrammaticCoreData/blob/main/Examples/Example_01/Example_01/Managers/NotesManager.swift) for basic CRUD operations
73+
7274
- [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`:
7375

7476
We create an `Author` entity programmatically:

0 commit comments

Comments
 (0)