@@ -21,7 +21,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
21
21
where condition: QueryPredicate ? = nil ,
22
22
completion: @escaping DataStoreCallback < M > ) {
23
23
log. verbose ( " Saving: \( model) with condition: \( String ( describing: condition) ) " )
24
- reinitStorageEngineIfNeeded ( )
24
+ initStorageEngineAndStartSync ( )
25
25
26
26
// TODO: Refactor this into a proper request/result where the result includes metadata like the derived
27
27
// mutation type
@@ -63,7 +63,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
63
63
public func query< M: Model > ( _ modelType: M . Type ,
64
64
byId id: String ,
65
65
completion: DataStoreCallback < M ? > ) {
66
- reinitStorageEngineIfNeeded ( )
66
+ initStorageEngineAndStartSync ( )
67
67
let predicate : QueryPredicate = field ( " id " ) == id
68
68
query ( modelType, where: predicate, paginate: . firstResult) {
69
69
switch $0 {
@@ -99,7 +99,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
99
99
sort sortInput: [ QuerySortDescriptor ] ? = nil ,
100
100
paginate paginationInput: QueryPaginationInput ? = nil ,
101
101
completion: DataStoreCallback < [ M ] > ) {
102
- reinitStorageEngineIfNeeded ( )
102
+ initStorageEngineAndStartSync ( )
103
103
storageEngine. query ( modelType,
104
104
modelSchema: modelSchema,
105
105
predicate: predicate,
@@ -120,7 +120,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
120
120
withId id: String ,
121
121
where predicate: QueryPredicate ? = nil ,
122
122
completion: @escaping DataStoreCallback < Void > ) {
123
- reinitStorageEngineIfNeeded ( )
123
+ initStorageEngineAndStartSync ( )
124
124
storageEngine. delete ( modelType, modelSchema: modelSchema, withId: id, predicate: predicate) { result in
125
125
self . onDeleteCompletion ( result: result, modelSchema: modelSchema, completion: completion)
126
126
}
@@ -136,7 +136,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
136
136
modelSchema: ModelSchema ,
137
137
where predicate: QueryPredicate ? = nil ,
138
138
completion: @escaping DataStoreCallback < Void > ) {
139
- reinitStorageEngineIfNeeded ( )
139
+ initStorageEngineAndStartSync ( )
140
140
storageEngine. delete ( type ( of: model) ,
141
141
modelSchema: modelSchema,
142
142
withId: model. id,
@@ -155,7 +155,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
155
155
modelSchema: ModelSchema ,
156
156
where predicate: QueryPredicate ,
157
157
completion: @escaping DataStoreCallback < Void > ) {
158
- reinitStorageEngineIfNeeded ( )
158
+ initStorageEngineAndStartSync ( )
159
159
let onCompletion : DataStoreCallback < [ M ] > = { result in
160
160
switch result {
161
161
case . success( let models) :
@@ -174,52 +174,57 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
174
174
}
175
175
176
176
public func start( completion: @escaping DataStoreCallback < Void > ) {
177
- reinitStorageEngineIfNeeded { result in
177
+ initStorageEngineAndStartSync { result in
178
178
completion ( result)
179
179
}
180
180
}
181
181
182
182
public func stop( completion: @escaping DataStoreCallback < Void > ) {
183
- storageEngineInitSemaphore. wait ( )
184
- operationQueue. operations. forEach { operation in
185
- if let operation = operation as? DataStoreObserveQueryOperation {
186
- operation. resetState ( )
183
+ storageEngineInitQueue. sync {
184
+ operationQueue. operations. forEach { operation in
185
+ if let operation = operation as? DataStoreObserveQueryOperation {
186
+ operation. resetState ( )
187
+ }
188
+ }
189
+ dispatchedModelSyncedEvents. forEach { _, dispatchedModelSynced in
190
+ dispatchedModelSynced. set ( false )
191
+ }
192
+ if storageEngine == nil {
193
+
194
+ completion ( . successfulVoid)
195
+ return
196
+ }
197
+
198
+ storageEngine. stopSync { result in
199
+ self . storageEngine = nil
200
+ completion ( result)
187
201
}
188
- }
189
- dispatchedModelSyncedEvents. forEach { _, dispatchedModelSynced in
190
- dispatchedModelSynced. set ( false )
191
- }
192
- if storageEngine == nil {
193
- storageEngineInitSemaphore. signal ( )
194
- completion ( . successfulVoid)
195
- return
196
- }
197
- storageEngineInitSemaphore. signal ( )
198
- storageEngine. stopSync { result in
199
- self . storageEngine = nil
200
- completion ( result)
201
202
}
202
203
}
203
204
204
205
public func clear( completion: @escaping DataStoreCallback < Void > ) {
205
- storageEngineInitSemaphore. wait ( )
206
- operationQueue. operations. forEach { operation in
207
- if let operation = operation as? DataStoreObserveQueryOperation {
208
- operation. resetState ( )
209
- }
210
- }
211
- dispatchedModelSyncedEvents. forEach { _, dispatchedModelSynced in
212
- dispatchedModelSynced. set ( false )
213
- }
214
- if storageEngine == nil {
215
- storageEngineInitSemaphore. signal ( )
216
- completion ( . successfulVoid)
206
+ if case let . failure( error) = initStorageEngine ( ) {
207
+ completion ( . failure( causedBy: error) )
217
208
return
218
209
}
219
- storageEngineInitSemaphore. signal ( )
220
- storageEngine. clear { result in
221
- self . storageEngine = nil
222
- completion ( result)
210
+
211
+ storageEngineInitQueue. sync {
212
+ operationQueue. operations. forEach { operation in
213
+ if let operation = operation as? DataStoreObserveQueryOperation {
214
+ operation. resetState ( )
215
+ }
216
+ }
217
+ dispatchedModelSyncedEvents. forEach { _, dispatchedModelSynced in
218
+ dispatchedModelSynced. set ( false )
219
+ }
220
+ if storageEngine == nil {
221
+ completion ( . successfulVoid)
222
+ return
223
+ }
224
+ storageEngine. clear { result in
225
+ self . storageEngine = nil
226
+ completion ( result)
227
+ }
223
228
}
224
229
}
225
230
0 commit comments