Skip to content

Commit dabc080

Browse files
committed
Use subscription API instead of notifications to handle UI updates in response to database changes.
1 parent 14ffad4 commit dabc080

10 files changed

+50
-201
lines changed

Example/NotesExample-iOS/AuthorCreationViewController.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ extension AuthorCreationViewController {
3939
if segue.identifier == "cancelAuthorDraft" {
4040
authorDraft = Author()
4141
} else if segue.identifier == "saveAuthorDraft" {
42-
let authorId = try! Services.instance.authorBox.put(authorDraft)
43-
NotificationCenter.default.post(name: .authorAdded, object: authorDraft, userInfo: [ "authorId" : authorId.value ])
42+
try! Services.instance.authorBox.put(authorDraft)
4443
}
4544
}
4645

Example/NotesExample-iOS/AuthorEditingViewController.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,7 @@ extension AuthorEditingViewController: UITextFieldDelegate {
7575
func renameAuthor(to newName: String) {
7676
guard let author = self.author else { return }
7777

78-
let oldName = author.name
7978
author.name = newName
8079
try! authorBox.put(author)
81-
82-
NotificationCenter.default.post(
83-
name: .authorNameDidChange,
84-
object: author,
85-
userInfo: [ "oldValue" : oldName, "newValue" : newName])
8680
}
8781
}

Example/NotesExample-iOS/AuthorsOverviewViewController.swift

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ class AuthorsOverviewViewController: UITableViewController {
1111
var authors = [Author]()
1212

1313
var authorBox: Box<Author> = Services.instance.authorBox
14-
15-
16-
private var authorAddedSubscription: NotificationToken!
17-
private var authorNameChangeSubscription: NotificationToken!
14+
private var authorBoxObserver: Observer?
1815

1916
override func viewDidLoad() {
2017
super.viewDidLoad()
@@ -28,34 +25,27 @@ class AuthorsOverviewViewController: UITableViewController {
2825
let controllers = split.viewControllers
2926
authorEditingViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? AuthorEditingViewController
3027
}
31-
32-
self.refreshAuthors()
33-
34-
authorAddedSubscription = NotificationCenter.default.observe(name: .authorAdded, object: nil) { _ in
35-
self.refreshAuthors()
36-
}
37-
38-
authorNameChangeSubscription = NotificationCenter.default.observe(name: .authorNameDidChange, object: nil) { _ in
39-
self.refreshAuthors()
40-
}
41-
}
42-
43-
private func refreshAuthors() {
44-
authors = authorBox.all()
45-
guard let tableView = self.tableView else { return }
46-
tableView.reloadData()
4728
}
4829

4930
override func viewWillAppear(_ animated: Bool) {
5031
clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
5132
super.viewWillAppear(animated)
33+
34+
authorBoxObserver = authorBox.subscribe { authors, _ in
35+
self.authors = authors
36+
guard let tableView = self.tableView else { return }
37+
tableView.reloadData()
38+
}
39+
}
40+
41+
override func viewWillDisappear(_ animated: Bool) {
42+
super.viewWillDisappear(animated)
43+
authorBoxObserver = nil
5244
}
5345

5446
private func deleteAuthor(at index: Int) {
5547
let authorId = authors[index].id
56-
authors.remove(at: index)
5748
try! authorBox.remove(authorId)
58-
NotificationCenter.default.post(name: .authorRemoved, object: nil, userInfo: [ "authorId" : authorId.value ])
5949
}
6050

6151
}
@@ -109,7 +99,6 @@ extension AuthorsOverviewViewController {
10999
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
110100
if editingStyle == .delete {
111101
deleteAuthor(at: indexPath.row)
112-
tableView.deleteRows(at: [indexPath], with: .fade)
113102
} else if editingStyle == .insert {
114103
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
115104
}

Example/NotesExample-iOS/MasterViewController.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,34 @@ import ObjectBox
66
class MasterViewController: UITableViewController {
77

88
lazy var noteBox: Box<Note> = Services.instance.noteBox
9+
var noteBoxObserver: Observer?
910
lazy var authorBox: Box<Author> = Services.instance.authorBox
10-
11-
private var noteAddedSubscription: NotificationToken!
12-
private var noteRemovedSubscription: NotificationToken!
13-
private var authorRemovedSubscription: NotificationToken!
14-
private var authorAddedSubscription: NotificationToken!
11+
var authorBoxObserver: Observer?
1512

1613
override func viewDidLoad() {
1714
super.viewDidLoad()
1815

1916
clearsSelectionOnViewWillAppear = true
17+
}
18+
19+
override func viewDidAppear(_ animated: Bool) {
20+
super.viewDidAppear(animated)
2021

21-
noteAddedSubscription = NotificationCenter.default.observe(name: .noteAdded, object: nil) { _ in
22-
self.tableView.reloadData()
23-
}
24-
25-
noteRemovedSubscription = NotificationCenter.default.observe(name: .noteRemoved, object: nil) { _ in
26-
self.tableView.reloadData()
27-
}
28-
29-
authorAddedSubscription = NotificationCenter.default.observe(name: .authorAdded, object: nil) { _ in
22+
noteBoxObserver = noteBox.subscribe {
3023
self.tableView.reloadData()
3124
}
32-
33-
authorRemovedSubscription = NotificationCenter.default.observe(name: .authorRemoved, object: nil) { _ in
25+
26+
authorBoxObserver = authorBox.subscribe {
3427
self.tableView.reloadData()
3528
}
3629
}
30+
31+
override func viewWillDisappear(_ animated: Bool) {
32+
super.viewWillDisappear(animated)
33+
34+
noteBoxObserver = nil
35+
authorBoxObserver = nil
36+
}
3737

3838
@IBAction func replaceWithDemoData(_ sender: Any?) {
3939
try! Services.instance.replaceWithDemoData()

Example/NotesExample-iOS/NoteEditingViewController.swift

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ class NoteEditingViewController: UITableViewController {
4242
@IBOutlet weak var creationDateLabel: UILabel!
4343
@IBOutlet weak var modificationDateLabel: UILabel!
4444

45-
private var noteModificationDateChangeSubscription: NotificationToken!
46-
4745
lazy var authorModel: AuthorModel = AuthorModel(authorBox: Services.instance.authorBox)
4846
var noteBox: Box<Note> = Services.instance.noteBox
4947

@@ -72,10 +70,6 @@ class NoteEditingViewController: UITableViewController {
7270

7371
self.refreshCreationDate()
7472
self.refreshModificationDate()
75-
76-
noteModificationDateChangeSubscription = NotificationCenter.default.observe(name: .noteModificationDateDidChange, object: nil) { _ in
77-
self.refreshModificationDate()
78-
}
7973
}
8074

8175
private func refreshCreationDate() {
@@ -134,8 +128,7 @@ extension NoteEditingViewController {
134128
self.note = nil
135129
} else if segue.identifier == "saveDraft" {
136130
guard let note = self.note else { preconditionFailure() }
137-
let noteId = try! noteBox.put(note)
138-
NotificationCenter.default.post(name: .noteAdded, object: note, userInfo: [ "noteId" : noteId.value ])
131+
try! noteBox.put(note)
139132
}
140133
}
141134

@@ -148,27 +141,20 @@ extension NoteEditingViewController: UITextFieldDelegate {
148141
func textFieldDidEndEditing(_ textField: UITextField) {
149142
let title = textField.text ?? ""
150143
changeNoteTitle(to: title)
144+
refreshAuthors()
151145
}
152146

153147
func changeNoteTitle(to newTitle: String) {
154148
guard let note = self.note else { return }
155149

156-
let oldTitle = note.title
157150
note.title = newTitle
158151

159152
// Do not autosave drafts
160153
guard self.mode == .edit else { return }
161154

162155
try! noteBox.put(note)
163156

164-
NotificationCenter.default.post(
165-
name: .noteTitleDidChange,
166-
object: note,
167-
userInfo: [ "oldValue" : oldTitle, "newValue" : newTitle])
168-
NotificationCenter.default.post(
169-
name: .noteModificationDateDidChange,
170-
object: note,
171-
userInfo: nil)
157+
refreshModificationDate()
172158
}
173159
}
174160

@@ -181,7 +167,7 @@ extension NoteEditingViewController: UIPickerViewDelegate {
181167
let pickerItems: [String]
182168

183169
init(authorBox: Box<Author>) {
184-
self.authors = authorBox.all().sorted(by: { $0.name < $1.name })
170+
self.authors = authorBox.all.sorted(by: { $0.name < $1.name })
185171
self.pickerItems = authors
186172
.map { $0.name }
187173
.prepending("(None)")
@@ -214,7 +200,6 @@ extension NoteEditingViewController: UIPickerViewDelegate {
214200
func changeNoteAuthor(to newAuthor: Author?) {
215201
guard let note = self.note else { return }
216202

217-
let oldAuthorId = note.author.targetId
218203
note.author.target = newAuthor
219204
note.modificationDate = Date()
220205

@@ -223,25 +208,8 @@ extension NoteEditingViewController: UIPickerViewDelegate {
223208

224209
try! noteBox.put(note)
225210

226-
let changeUserInfo: [String: Any] = {
227-
var result: [String: Any] = [:]
228-
if let oldAuthorId = oldAuthorId {
229-
result["oldValue"] = oldAuthorId.value
230-
}
231-
if let newAuthorId = newAuthor?.id {
232-
result["newValue"] = newAuthorId.value
233-
}
234-
return result
235-
}()
236-
237-
NotificationCenter.default.post(
238-
name: .noteAuthorDidChange,
239-
object: note,
240-
userInfo: changeUserInfo)
241-
NotificationCenter.default.post(
242-
name: .noteModificationDateDidChange,
243-
object: note,
244-
userInfo: nil)
211+
refreshModificationDate()
212+
refreshAuthors()
245213
}
246214
}
247215

@@ -268,10 +236,7 @@ extension NoteEditingViewController: UITextViewDelegate {
268236

269237
func textViewDidChange(_ textView: UITextView) {
270238
note?.text = textView.text
271-
NotificationCenter.default.post(
272-
name: .noteModificationDateDidChange,
273-
object: note,
274-
userInfo: nil)
239+
refreshModificationDate()
275240
}
276241

277242
func textViewDidEndEditing(_ textView: UITextView) {

Example/NotesExample-iOS/NotesOverviewViewController.swift

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class NotesOverviewViewController: UITableViewController {
1818

1919
let authorId: Id<Author>?
2020
let query: Query<Note>
21+
var queryObserver: Observer?
2122

2223
init(authorId: Id<Author>?, noteBox: Box<Note> = Services.instance.noteBox) {
2324
self.authorId = authorId
@@ -30,19 +31,14 @@ class NotesOverviewViewController: UITableViewController {
3031
}
3132

3233
func notes() -> [Note] {
33-
return query.find()
34+
return try! query.find()
3435
}
3536
}
3637

3738
func filterBy(authorId: Id<Author>?) {
3839
filter = Filter(authorId: authorId)
3940
}
4041

41-
private var noteAddedSubscription: NotificationToken!
42-
private var noteRemovedSubscription: NotificationToken!
43-
private var noteTitleChangeSubscription: NotificationToken!
44-
private var noteAuthorChangeSubscription: NotificationToken!
45-
4642
override func viewDidLoad() {
4743
super.viewDidLoad()
4844

@@ -55,26 +51,19 @@ class NotesOverviewViewController: UITableViewController {
5551
let controllers = split.viewControllers
5652
noteViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? NoteEditingViewController
5753
}
58-
59-
refreshNotes()
60-
61-
noteAddedSubscription = NotificationCenter.default.observe(name: .noteAdded, object: nil) { _ in
62-
self.refreshNotes()
63-
}
64-
65-
noteRemovedSubscription = NotificationCenter.default.observe(name: .noteRemoved, object: nil) { notification in
66-
guard notification.object as AnyObject !== self else { return }
67-
self.refreshNotes()
68-
}
69-
70-
noteTitleChangeSubscription = NotificationCenter.default.observe(name: .noteTitleDidChange, object: nil) { _ in
71-
self.refreshNotes()
72-
}
73-
74-
noteAuthorChangeSubscription = NotificationCenter.default.observe(name: .noteAuthorDidChange, object: nil) { _ in
75-
self.refreshNotes()
54+
}
55+
56+
override func viewDidAppear(_ animated: Bool) {
57+
filter.queryObserver = filter.query.subscribe { notes, _ in
58+
self.notes = notes
59+
guard let tableView = self.tableView else { return }
60+
tableView.reloadData()
7661
}
7762
}
63+
64+
override func viewWillDisappear(_ animated: Bool) {
65+
filter.queryObserver = nil
66+
}
7867

7968
private func refreshNotes() {
8069
notes = filter.notes()
@@ -89,9 +78,7 @@ class NotesOverviewViewController: UITableViewController {
8978

9079
private func deleteNote(at index: Int) {
9180
let noteId = notes[index].id
92-
notes.remove(at: index)
9381
try! noteBox.remove(noteId)
94-
NotificationCenter.default.post(name: .noteRemoved, object: self, userInfo: [ "noteId" : noteId.value ])
9582
}
9683

9784
}

Example/NotesExample-iOS/Notification+AuthorChanges.swift

Lines changed: 0 additions & 14 deletions
This file was deleted.

Example/NotesExample-iOS/Notification+NoteChanges.swift

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)