forked from objcio/core-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSManagedObjectContext+Observers.swift
174 lines (127 loc) · 5.86 KB
/
NSManagedObjectContext+Observers.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// Notifications.swift
// Moody
//
// Created by Daniel Eggert on 24/05/2015.
// Copyright (c) 2015 objc.io. All rights reserved.
//
import Foundation
import CoreData
public struct ContextDidSaveNotification {
public init(note: Notification) {
guard note.name == .NSManagedObjectContextDidSave else { fatalError() }
notification = note
}
public var insertedObjects: AnyIterator<NSManagedObject> {
return iterator(forKey: NSInsertedObjectsKey)
}
public var updatedObjects: AnyIterator<NSManagedObject> {
return iterator(forKey: NSUpdatedObjectsKey)
}
public var deletedObjects: AnyIterator<NSManagedObject> {
return iterator(forKey: NSDeletedObjectsKey)
}
public var managedObjectContext: NSManagedObjectContext {
guard let c = notification.object as? NSManagedObjectContext else { fatalError("Invalid notification object") }
return c
}
// MARK: Private
fileprivate let notification: Notification
fileprivate func iterator(forKey key: String) -> AnyIterator<NSManagedObject> {
guard let set = (notification as Notification).userInfo?[key] as? NSSet else {
return AnyIterator { nil }
}
var innerIterator = set.makeIterator()
return AnyIterator { return innerIterator.next() as? NSManagedObject }
}
}
extension ContextDidSaveNotification: CustomDebugStringConvertible {
public var debugDescription: String {
var components = [notification.name.rawValue]
components.append(managedObjectContext.description)
for (name, set) in [("inserted", insertedObjects), ("updated", updatedObjects), ("deleted", deletedObjects)] {
let all = set.map { $0.objectID.description }.joined(separator: ", ")
components.append("\(name): {\(all)})")
}
return components.joined(separator: " ")
}
}
public struct ContextWillSaveNotification {
public init(note: Notification) {
assert(note.name == .NSManagedObjectContextWillSave)
notification = note
}
public var managedObjectContext: NSManagedObjectContext {
guard let c = notification.object as? NSManagedObjectContext else { fatalError("Invalid notification object") }
return c
}
// MARK: Private
fileprivate let notification: Notification
}
public struct ObjectsDidChangeNotification {
init(note: Notification) {
assert(note.name == .NSManagedObjectContextObjectsDidChange)
notification = note
}
public var insertedObjects: Set<NSManagedObject> {
return objects(forKey: NSInsertedObjectsKey)
}
public var updatedObjects: Set<NSManagedObject> {
return objects(forKey: NSUpdatedObjectsKey)
}
public var deletedObjects: Set<NSManagedObject> {
return objects(forKey: NSDeletedObjectsKey)
}
public var refreshedObjects: Set<NSManagedObject> {
return objects(forKey: NSRefreshedObjectsKey)
}
public var invalidatedObjects: Set<NSManagedObject> {
return objects(forKey: NSInvalidatedObjectsKey)
}
public var invalidatedAllObjects: Bool {
return (notification as Notification).userInfo?[NSInvalidatedAllObjectsKey] != nil
}
public var managedObjectContext: NSManagedObjectContext {
guard let c = notification.object as? NSManagedObjectContext else { fatalError("Invalid notification object") }
return c
}
// MARK: Private
fileprivate let notification: Notification
fileprivate func objects(forKey key: String) -> Set<NSManagedObject> {
return ((notification as Notification).userInfo?[key] as? Set<NSManagedObject>) ?? Set()
}
}
extension NSManagedObjectContext {
/// Adds the given block to the default `NotificationCenter`'s dispatch table for the given context's did-save notifications.
/// - returns: An opaque object to act as the observer. This must be sent to the default `NotificationCenter`'s `removeObserver()`.
public func addContextDidSaveNotificationObserver(_ handler: @escaping (ContextDidSaveNotification) -> ()) -> NSObjectProtocol {
let nc = NotificationCenter.default
return nc.addObserver(forName: .NSManagedObjectContextDidSave, object: self, queue: nil) { note in
let wrappedNote = ContextDidSaveNotification(note: note)
handler(wrappedNote)
}
}
/// Adds the given block to the default `NotificationCenter`'s dispatch table for the given context's will-save notifications.
/// - returns: An opaque object to act as the observer. This must be sent to the default `NotificationCenter`'s `removeObserver()`.
public func addContextWillSaveNotificationObserver(_ handler: @escaping (ContextWillSaveNotification) -> ()) -> NSObjectProtocol {
let nc = NotificationCenter.default
return nc.addObserver(forName: .NSManagedObjectContextWillSave, object: self, queue: nil) { note in
let wrappedNote = ContextWillSaveNotification(note: note)
handler(wrappedNote)
}
}
/// Adds the given block to the default `NotificationCenter`'s dispatch table for the given context's objects-did-change notifications.
/// - returns: An opaque object to act as the observer. This must be sent to the default `NotificationCenter`'s `removeObserver()`.
public func addObjectsDidChangeNotificationObserver(_ handler: @escaping (ObjectsDidChangeNotification) -> ()) -> NSObjectProtocol {
let nc = NotificationCenter.default
return nc.addObserver(forName: .NSManagedObjectContextObjectsDidChange, object: self, queue: nil) { note in
let wrappedNote = ObjectsDidChangeNotification(note: note)
handler(wrappedNote)
}
}
public func performMergeChanges(from note: ContextDidSaveNotification) {
perform {
self.mergeChanges(fromContextDidSave: note.notification)
}
}
}