@@ -102,6 +102,57 @@ open class Subscription<T: ParseObject>: ParseSubscription, ObservableObject {
102
102
}
103
103
}
104
104
105
+ /// The objects found in a `find`, `first`, or `aggregate`
106
+ /// query.
107
+ /// - note: this will only countain one item for `first`.
108
+ public internal( set) var results : [ T ] ? {
109
+ willSet {
110
+ if newValue != nil {
111
+ resultsCodable = nil
112
+ count = nil
113
+ error = nil
114
+ objectWillChange. send ( )
115
+ }
116
+ }
117
+ }
118
+
119
+ /// The number of items found in a `count` query.
120
+ public internal( set) var count : Int ? {
121
+ willSet {
122
+ if newValue != nil {
123
+ results = nil
124
+ resultsCodable = nil
125
+ error = nil
126
+ objectWillChange. send ( )
127
+ }
128
+ }
129
+ }
130
+
131
+ /// Results of a `explain` or `hint` query.
132
+ public internal( set) var resultsCodable : AnyCodable ? {
133
+ willSet {
134
+ if newValue != nil {
135
+ results = nil
136
+ count = nil
137
+ error = nil
138
+ objectWillChange. send ( )
139
+ }
140
+ }
141
+ }
142
+
143
+ /// If an error occured during a `find`, `first`, `count`, or `aggregate`
144
+ /// query.
145
+ public internal( set) var error : ParseError ? {
146
+ willSet {
147
+ if newValue != nil {
148
+ count = nil
149
+ results = nil
150
+ resultsCodable = nil
151
+ objectWillChange. send ( )
152
+ }
153
+ }
154
+ }
155
+
105
156
/**
106
157
Creates a new subscription that can be used to handle updates.
107
158
*/
@@ -128,6 +179,143 @@ open class Subscription<T: ParseObject>: ParseSubscription, ObservableObject {
128
179
open func didUnsubscribe( ) {
129
180
self . unsubscribed = query
130
181
}
182
+
183
+ /**
184
+ Finds objects and publishes them as `results` afterwards.
185
+
186
+ - parameter options: A set of header options sent to the server. Defaults to an empty set.
187
+ - parameter callbackQueue: The queue to return to after completion. Default value of `.main`.
188
+ */
189
+ open func find( options: API . Options = [ ] , callbackQueue: DispatchQueue = . main) {
190
+ query. find ( options: options, callbackQueue: callbackQueue) { result in
191
+ switch result {
192
+
193
+ case . success( let results) :
194
+ self . results = results
195
+ case . failure( let error) :
196
+ self . error = error
197
+ }
198
+ }
199
+ }
200
+
201
+ /**
202
+ Finds objects and publishes them as `resultsCodable` afterwards.
203
+
204
+ - parameter explain: Used to toggle the information on the query plan.
205
+ - parameter hint: String or Object of index that should be used when executing query.
206
+ - parameter options: A set of header options sent to the server. Defaults to an empty set.
207
+ - parameter callbackQueue: The queue to return to after completion. Default value of .main.
208
+ */
209
+ open func find( explain: Bool , hint: String ? = nil , options: API . Options = [ ] , callbackQueue: DispatchQueue = . main) {
210
+ query. find ( explain: explain, hint: hint, options: options, callbackQueue: callbackQueue) { result in
211
+ switch result {
212
+
213
+ case . success( let results) :
214
+ self . resultsCodable = results
215
+ case . failure( let error) :
216
+ self . error = error
217
+ }
218
+ }
219
+ }
220
+
221
+ /**
222
+ Gets an object and publishes them as `results` afterwards.
223
+
224
+ - warning: This method mutates the query. It will reset the limit to `1`.
225
+ - parameter options: A set of header options sent to the server. Defaults to an empty set.
226
+ - parameter callbackQueue: The queue to return to after completion. Default value of `.main`.
227
+ */
228
+ open func first( options: API . Options = [ ] , callbackQueue: DispatchQueue = . main) {
229
+ query. first ( options: options, callbackQueue: callbackQueue) { result in
230
+ switch result {
231
+
232
+ case . success( let results) :
233
+ self . results = [ results]
234
+ case . failure( let error) :
235
+ self . error = error
236
+ }
237
+ }
238
+ }
239
+
240
+ /**
241
+ Gets an object and publishes them as `resultsCodable` afterwards.
242
+
243
+ - warning: This method mutates the query. It will reset the limit to `1`.
244
+ - parameter explain: Used to toggle the information on the query plan.
245
+ - parameter hint: String or Object of index that should be used when executing query.
246
+ - parameter options: A set of header options sent to the server. Defaults to an empty set.
247
+ - parameter callbackQueue: The queue to return to after completion. Default value of `.main`.
248
+ */
249
+ open func first( explain: Bool , hint: String ? = nil , options: API . Options = [ ] , callbackQueue: DispatchQueue = . main) {
250
+ query. first ( explain: explain, hint: hint, options: options, callbackQueue: callbackQueue) { result in
251
+ switch result {
252
+
253
+ case . success( let results) :
254
+ self . resultsCodable = results
255
+ case . failure( let error) :
256
+ self . error = error
257
+ }
258
+ }
259
+ }
260
+
261
+ /**
262
+ Counts objects and publishes them as `count` afterwards.
263
+
264
+ - parameter options: A set of header options sent to the server. Defaults to an empty set.
265
+ - parameter callbackQueue: The queue to return to after completion. Default value of `.main`.
266
+ */
267
+ open func count( options: API . Options = [ ] , callbackQueue: DispatchQueue = . main) {
268
+ query. count ( options: options, callbackQueue: callbackQueue) { result in
269
+ switch result {
270
+
271
+ case . success( let results) :
272
+ self . count = results
273
+ case . failure( let error) :
274
+ self . error = error
275
+ }
276
+ }
277
+ }
278
+
279
+ /**
280
+ Counts objects and publishes them as `resultsCodable` afterwards.
281
+ - parameter explain: Used to toggle the information on the query plan.
282
+ - parameter hint: String or Object of index that should be used when executing query.
283
+ - parameter options: A set of header options sent to the server. Defaults to an empty set.
284
+ - parameter callbackQueue: The queue to return to after completion. Default value of `.main`.
285
+ */
286
+ open func count( explain: Bool , hint: String ? = nil , options: API . Options = [ ] , callbackQueue: DispatchQueue = . main) {
287
+ query. count ( explain: explain, hint: hint, options: options) { result in
288
+ switch result {
289
+
290
+ case . success( let results) :
291
+ self . resultsCodable = results
292
+ case . failure( let error) :
293
+ self . error = error
294
+ }
295
+ }
296
+ }
297
+
298
+ /**
299
+ Executes an aggregate query and publishes the results as `results` afterwards.
300
+ - requires: `.useMasterKey` has to be available and passed as one of the set of `options`.
301
+ - parameter pipeline: A pipeline of stages to process query.
302
+ - parameter options: A set of header options sent to the server. Defaults to an empty set.
303
+ - parameter callbackQueue: The queue to return to after completion. Default value of `.main`.
304
+ - warning: This hasn't been tested thoroughly.
305
+ */
306
+ open func aggregate( _ pipeline: Query < T > . AggregateType ,
307
+ options: API . Options = [ ] ,
308
+ callbackQueue: DispatchQueue = . main) {
309
+ query. aggregate ( pipeline, options: options, callbackQueue: callbackQueue) { result in
310
+ switch result {
311
+
312
+ case . success( let results) :
313
+ self . results = results
314
+ case . failure( let error) :
315
+ self . error = error
316
+ }
317
+ }
318
+ }
131
319
}
132
320
#endif
133
321
0 commit comments