Skip to content

Commit 6b2c476

Browse files
committed
Initial with tests
1 parent d03861f commit 6b2c476

File tree

3 files changed

+446
-4
lines changed

3 files changed

+446
-4
lines changed

Sources/ParseSwift/LiveQuery/Subscription.swift

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,57 @@ open class Subscription<T: ParseObject>: ParseSubscription, ObservableObject {
102102
}
103103
}
104104

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+
105156
/**
106157
Creates a new subscription that can be used to handle updates.
107158
*/
@@ -128,6 +179,143 @@ open class Subscription<T: ParseObject>: ParseSubscription, ObservableObject {
128179
open func didUnsubscribe() {
129180
self.unsubscribed = query
130181
}
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+
}
131319
}
132320
#endif
133321

Sources/ParseSwift/Types/Query.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ extension Query: Queryable {
853853
- parameter options: A set of header options sent to the server. Defaults to an empty set.
854854
- parameter callbackQueue: The queue to return to after completion. Default value of `.main`.
855855
- parameter completion: The block to execute.
856-
It should have the following argument signature: `(Result<[ResultType], ParseError>)`
856+
It should have the following argument signature: `(Result<[ResultType], ParseError>)`.
857857
*/
858858
public func find(options: API.Options = [], callbackQueue: DispatchQueue = .main,
859859
completion: @escaping (Result<[ResultType], ParseError>) -> Void) {
@@ -872,7 +872,7 @@ extension Query: Queryable {
872872
- parameter options: A set of header options sent to the server. Defaults to an empty set.
873873
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
874874
- parameter completion: The block to execute.
875-
It should have the following argument signature: `(Result<[AnyResultType], ParseError>)`
875+
It should have the following argument signature: `(Result<[AnyResultType], ParseError>)`.
876876
*/
877877
public func find(explain: Bool, hint: String? = nil, options: API.Options = [],
878878
callbackQueue: DispatchQueue = .main,
@@ -1012,7 +1012,7 @@ extension Query: Queryable {
10121012
- parameter options: A set of header options sent to the server. Defaults to an empty set.
10131013
- parameter callbackQueue: The queue to return to after completion. Default value of `.main`.
10141014
- parameter completion: The block to execute.
1015-
It should have the following argument signature: `(Result<Int, ParseError>)`
1015+
It should have the following argument signature: `(Result<Int, ParseError>)`.
10161016
*/
10171017
public func count(explain: Bool, hint: String? = nil, options: API.Options = [],
10181018
callbackQueue: DispatchQueue = .main,
@@ -1059,7 +1059,7 @@ extension Query: Queryable {
10591059
- parameter options: A set of header options sent to the server. Defaults to an empty set.
10601060
- parameter callbackQueue: The queue to return to after completion. Default value of `.main`.
10611061
- parameter completion: The block to execute.
1062-
It should have the following argument signature: `(Result<Int, ParseError>)`
1062+
It should have the following argument signature: `(Result<[ResultType], ParseError>)`.
10631063
- warning: This hasn't been tested thoroughly.
10641064
*/
10651065
public func aggregate(_ pipeline: AggregateType,

0 commit comments

Comments
 (0)