@@ -52,35 +52,39 @@ public class ManagedObjectObserver<T:NSManagedObject>: NSObject {
52
52
self . subscribers = [ Subscriber] ( )
53
53
super. init ( )
54
54
55
- notificationObserver = NotificationCenter . default. addObserver ( forName: NSNotification . Name. NSManagedObjectContextObjectsDidChange, object: context, queue: nil ) { [ unowned self] notification in
55
+ notificationObserver = NotificationCenter . default. addObserver ( forName: NSNotification . Name. NSManagedObjectContextObjectsDidChange, object: context, queue: nil ) { [ weak self] notification in
56
+ guard let strongSelf = self else {
57
+ return
58
+ }
59
+
56
60
context. perform {
57
- if self . subscribers. isEmpty {
61
+ if strongSelf . subscribers. isEmpty {
58
62
return
59
63
}
60
64
61
65
do {
62
- let convertedObject = try context. find ( T . self, managedObjectID: self . observedObject. objectID)
66
+ let convertedObject = try context. find ( T . self, managedObjectID: strongSelf . observedObject. objectID)
63
67
if let updatedObjects = ( notification as NSNotification ) . userInfo ? [ NSUpdatedObjectsKey] as? NSSet {
64
68
if updatedObjects. contains ( convertedObject) {
65
- self . notifySubscribers ( . updated( convertedObject) )
69
+ strongSelf . notifySubscribers ( . updated( convertedObject) )
66
70
}
67
71
}
68
72
69
73
if let refreshedObjects = ( notification as NSNotification ) . userInfo ? [ NSRefreshedObjectsKey] as? NSSet {
70
74
if refreshedObjects. contains ( convertedObject) {
71
- self . notifySubscribers ( . refreshed( convertedObject) )
75
+ strongSelf . notifySubscribers ( . refreshed( convertedObject) )
72
76
}
73
77
}
74
78
75
79
if let insertedObjects = ( notification as NSNotification ) . userInfo ? [ NSInsertedObjectsKey] as? NSSet {
76
80
if insertedObjects. contains ( convertedObject) {
77
- self . notifySubscribers ( . inserted( convertedObject) )
81
+ strongSelf . notifySubscribers ( . inserted( convertedObject) )
78
82
}
79
83
}
80
84
81
85
if let deletedObjects = ( notification as NSNotification ) . userInfo ? [ NSDeletedObjectsKey] as? NSSet {
82
86
if deletedObjects. contains ( convertedObject) {
83
- self . notifySubscribers ( . deleted)
87
+ strongSelf . notifySubscribers ( . deleted)
84
88
}
85
89
}
86
90
}
0 commit comments