-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathNSManagedObjectContext+Observers.swift
More file actions
173 lines (127 loc) · 5.84 KB
/
NSManagedObjectContext+Observers.swift
File metadata and controls
173 lines (127 loc) · 5.84 KB
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: NSNotification) {
assert(note.name == NSManagedObjectContextDidSaveNotification)
notification = note
}
public var insertedObjects: AnyGenerator<ManagedObject> {
return generatorForKey(NSInsertedObjectsKey)
}
public var updatedObjects: AnyGenerator<ManagedObject> {
return generatorForKey(NSUpdatedObjectsKey)
}
public var deletedObjects: AnyGenerator<ManagedObject> {
return generatorForKey(NSDeletedObjectsKey)
}
public var managedObjectContext: NSManagedObjectContext {
guard let c = notification.object as? NSManagedObjectContext else { fatalError("Invalid notification object") }
return c
}
// MARK: Private
private let notification: NSNotification
private func generatorForKey(key: String) -> AnyGenerator<ManagedObject> {
guard let set = notification.userInfo?[key] as? NSSet else {
return anyGenerator { nil }
}
let innerGenerator = set.generate()
return anyGenerator { return innerGenerator.next() as? ManagedObject }
}
}
extension ContextDidSaveNotification: CustomDebugStringConvertible {
public var debugDescription: String {
var components = [notification.name]
components.append(managedObjectContext.description)
for (name, set) in [("inserted", insertedObjects), ("updated", updatedObjects), ("deleted", deletedObjects)] {
let all = set.map { $0.objectID.description }.joinWithSeparator(", ")
components.append("\(name): {\(all)}")
}
return components.joinWithSeparator(" ")
}
}
public struct ContextWillSaveNotification {
public init(note: NSNotification) {
assert(note.name == NSManagedObjectContextWillSaveNotification)
notification = note
}
public var managedObjectContext: NSManagedObjectContext {
guard let c = notification.object as? NSManagedObjectContext else { fatalError("Invalid notification object") }
return c
}
// MARK: Private
private let notification: NSNotification
}
public struct ObjectsDidChangeNotification {
init(note: NSNotification) {
assert(note.name == NSManagedObjectContextObjectsDidChangeNotification)
notification = note
}
public var insertedObjects: Set<ManagedObject> {
return objectsForKey(NSInsertedObjectsKey)
}
public var updatedObjects: Set<ManagedObject> {
return objectsForKey(NSUpdatedObjectsKey)
}
public var deletedObjects: Set<ManagedObject> {
return objectsForKey(NSDeletedObjectsKey)
}
public var refreshedObjects: Set<ManagedObject> {
return objectsForKey(NSRefreshedObjectsKey)
}
public var invalidatedObjects: Set<ManagedObject> {
return objectsForKey(NSInvalidatedObjectsKey)
}
public var invalidatedAllObjects: Bool {
return notification.userInfo?[NSInvalidatedAllObjectsKey] != nil
}
public var managedObjectContext: NSManagedObjectContext {
guard let c = notification.object as? NSManagedObjectContext else { fatalError("Invalid notification object") }
return c
}
// MARK: Private
private let notification: NSNotification
private func objectsForKey(key: String) -> Set<ManagedObject> {
return (notification.userInfo?[key] as? Set<ManagedObject>) ?? Set()
}
}
extension NSManagedObjectContext {
/// Adds the given block to the default `NSNotificationCenter`'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 `NSNotificationCenter`'s `removeObserver()`.
public func addContextDidSaveNotificationObserver(handler: ContextDidSaveNotification -> ()) -> NSObjectProtocol {
let nc = NSNotificationCenter.defaultCenter()
return nc.addObserverForName(NSManagedObjectContextDidSaveNotification, object: self, queue: nil) { note in
let wrappedNote = ContextDidSaveNotification(note: note)
handler(wrappedNote)
}
}
/// Adds the given block to the default `NSNotificationCenter`'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 `NSNotificationCenter`'s `removeObserver()`.
public func addContextWillSaveNotificationObserver(handler: ContextWillSaveNotification -> ()) -> NSObjectProtocol {
let nc = NSNotificationCenter.defaultCenter()
return nc.addObserverForName(NSManagedObjectContextWillSaveNotification, object: self, queue: nil) { note in
let wrappedNote = ContextWillSaveNotification(note: note)
handler(wrappedNote)
}
}
/// Adds the given block to the default `NSNotificationCenter`'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 `NSNotificationCenter`'s `removeObserver()`.
public func addObjectsDidChangeNotificationObserver(handler: ObjectsDidChangeNotification -> ()) -> NSObjectProtocol {
let nc = NSNotificationCenter.defaultCenter()
return nc.addObserverForName(NSManagedObjectContextObjectsDidChangeNotification, object: self, queue: nil) { note in
let wrappedNote = ObjectsDidChangeNotification(note: note)
handler(wrappedNote)
}
}
public func performMergeChangesFromContextDidSaveNotification(note: ContextDidSaveNotification) {
performBlock {
self.mergeChangesFromContextDidSaveNotification(note.notification)
}
}
}