@@ -105,14 +105,12 @@ open class OSModelStore<TModel: OSModel>: NSObject {
105
105
106
106
// listen for changes to this model
107
107
model. changeNotifier. subscribe ( self )
108
-
109
- guard !hydrating else {
110
- return
111
- }
112
-
113
- self . changeSubscription. fire { modelStoreListener in
114
- modelStoreListener. onAdded ( model)
115
- }
108
+ }
109
+ guard !hydrating else {
110
+ return
111
+ }
112
+ self . changeSubscription. fire { modelStoreListener in
113
+ modelStoreListener. onAdded ( model)
116
114
}
117
115
}
118
116
@@ -121,24 +119,28 @@ open class OSModelStore<TModel: OSModel>: NSObject {
121
119
This can happen if remove email or SMS is called and it doesn't exist in the store.
122
120
*/
123
121
public func remove( _ id: String ) {
122
+ var model : TModel ?
124
123
lock. withLock {
125
124
OneSignalLog . onesignalLog ( . LL_VERBOSE, message: " OSModelStore remove() called with model \( id) " )
126
- if let model = models [ id] {
125
+ if let foundModel = models [ id] {
126
+ model = foundModel
127
127
models. removeValue ( forKey: id)
128
128
129
129
// persist the models (with removed model) to storage
130
130
OneSignalUserDefaults . initShared ( ) . saveCodeableData ( forKey: self . storeKey, withValue: self . models)
131
-
132
- // no longer listen for changes to this model
133
- model. changeNotifier. unsubscribe ( self )
134
-
135
- self . changeSubscription. fire { modelStoreListener in
136
- modelStoreListener. onRemoved ( model)
137
- }
138
131
} else {
139
132
OneSignalLog . onesignalLog ( . LL_ERROR, message: " OSModelStore cannot remove \( id) because it doesn't exist in the store. " )
133
+ return
140
134
}
141
135
}
136
+ guard let model = model else {
137
+ return
138
+ }
139
+ // no longer listen for changes to this model
140
+ model. changeNotifier. unsubscribe ( self )
141
+ self . changeSubscription. fire { modelStoreListener in
142
+ modelStoreListener. onRemoved ( model)
143
+ }
142
144
}
143
145
144
146
/**
@@ -167,13 +169,12 @@ extension OSModelStore: OSModelChangedHandler {
167
169
// persist the changed models to storage
168
170
lock. withLock {
169
171
OneSignalUserDefaults . initShared ( ) . saveCodeableData ( forKey: self . storeKey, withValue: self . models)
170
-
171
- guard !hydrating else {
172
- return
173
- }
174
- self . changeSubscription. fire { modelStoreListener in
175
- modelStoreListener. onUpdated ( args)
176
- }
172
+ }
173
+ guard !hydrating else {
174
+ return
175
+ }
176
+ self . changeSubscription. fire { modelStoreListener in
177
+ modelStoreListener. onUpdated ( args)
177
178
}
178
179
}
179
180
}
0 commit comments