Skip to content

Commit b0bdf71

Browse files
authored
fix: use select for ParseLiveQuery when fields are not present (#377)
* fix: use select for ParseLiveQuery when fields are not present * nits * more nits * Use select in playgrounds instead of fields.
1 parent 4ccb8a6 commit b0bdf71

File tree

6 files changed

+40
-7
lines changed

6 files changed

+40
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/4.7.0...main)
66
* _Contributing to this repo? Add info about your change here to be included in the next release_
77

8+
__Fixes__
9+
- Use select for ParseLiveQuery when fields are not present ([#376](https://github.com/parse-community/Parse-Swift/pull/376)), thanks to [Corey Baker](https://github.com/cbaker6).
10+
811
### 4.7.0
912
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/4.6.0...4.7.0)
1013

ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ ParseLiveQuery.client?.sendPing { error in
148148
var query2 = GameScore.query("points" > 50)
149149

150150
//: Select the fields you are interested in receiving.
151-
query2.fields("points")
151+
query2.select("points")
152152

153153
//: Subscribe to your new query.
154154
let subscription2 = query2.subscribeCallback!

Sources/ParseSwift/LiveQuery/Messages.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ struct SubscribeMessage<T: ParseObject>: LiveQueryable, Encodable {
5757
self.op = operation
5858
self.requestId = requestId.value
5959
if let query = query {
60-
self.query = SubscribeQuery(className: query.className, where: query.where, fields: query.fields)
60+
self.query = SubscribeQuery(className: query.className,
61+
where: query.where,
62+
fields: query.fields ?? query.keys)
6163
}
6264
self.sessionToken = BaseParseUser.currentContainer?.sessionToken
6365
}

Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,9 @@ extension ParseLiveQuery {
795795
public func subscribe<T>(_ handler: T) throws -> T where T: QuerySubscribable {
796796

797797
let requestId = requestIdGenerator()
798-
let message = SubscribeMessage<T.Object>(operation: .subscribe, requestId: requestId, query: handler.query)
798+
let message = SubscribeMessage<T.Object>(operation: .subscribe,
799+
requestId: requestId,
800+
query: handler.query)
799801
guard let subscriptionRecord = SubscriptionRecord(
800802
query: handler.query,
801803
message: message,

Sources/ParseSwift/Types/Query.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,11 @@ public struct Query<T>: ParseTypeable where T: ParseObject {
353353
/**
354354
Make the query restrict the fields of the returned `ParseObject`s to include only the provided keys.
355355
If this is called multiple times, then all of the keys specified in each of the calls will be included.
356-
- parameter keys: A variadic list of keys include in the result.
356+
- parameter keys: A variadic list of keys to include in the result.
357357
- returns: The mutated instance of query for easy chaining.
358358
- warning: Requires Parse Server 5.0.0+.
359+
- note: When using the `Query` for `ParseLiveQuery`, setting `fields` will take precedence
360+
over `select`. If `fields` are not set, the `select` keys will be used.
359361
*/
360362
public func select(_ keys: String...) -> Query<T> {
361363
self.select(keys)
@@ -367,6 +369,8 @@ public struct Query<T>: ParseTypeable where T: ParseObject {
367369
- parameter keys: An array of keys to include in the result.
368370
- returns: The mutated instance of query for easy chaining.
369371
- warning: Requires Parse Server 5.0.0+.
372+
- note: When using the `Query` for `ParseLiveQuery`, setting `fields` will take precedence
373+
over `select`. If `fields` are not set, the `select` keys will be used.
370374
*/
371375
public func select(_ keys: [String]) -> Query<T> {
372376
var mutableQuery = self
@@ -399,13 +403,16 @@ public struct Query<T>: ParseTypeable where T: ParseObject {
399403
}
400404

401405
/**
402-
A variadic list of fields to receive when receiving a `ParseLiveQuery`.
406+
A variadic list of selected fields to receive updates on when the `Query` is used as a
407+
`ParseLiveQuery`.
403408

404409
Suppose the `ParseObject` Player contains three fields name, id and age.
405410
If you are only interested in the change of the name field, you can set `query.fields` to "name".
406411
In this situation, when the change of a Player `ParseObject` fulfills the subscription, only the
407412
name field will be sent to the clients instead of the full Player `ParseObject`.
408413
If this is called multiple times, then all of the keys specified in each of the calls will be received.
414+
- note: Setting `fields` will take precedence over `select`. If `fields` are not set, the
415+
`select` keys will be used.
409416
- warning: This is only for `ParseLiveQuery`.
410417
- parameter keys: A variadic list of fields to receive back instead of the whole `ParseObject`.
411418
- returns: The mutated instance of query for easy chaining.
@@ -415,13 +422,16 @@ public struct Query<T>: ParseTypeable where T: ParseObject {
415422
}
416423

417424
/**
418-
A list of fields to receive when receiving a `ParseLiveQuery`.
425+
A list of fields to receive updates on when the `Query` is used as a
426+
`ParseLiveQuery`.
419427

420428
Suppose the `ParseObject` Player contains three fields name, id and age.
421429
If you are only interested in the change of the name field, you can set `query.fields` to "name".
422430
In this situation, when the change of a Player `ParseObject` fulfills the subscription, only the
423431
name field will be sent to the clients instead of the full Player `ParseObject`.
424432
If this is called multiple times, then all of the keys specified in each of the calls will be received.
433+
- note: Setting `fields` will take precedence over `select`. If `fields` are not set, the
434+
`select` keys will be used.
425435
- warning: This is only for `ParseLiveQuery`.
426436
- parameter keys: An array of fields to receive back instead of the whole `ParseObject`.
427437
- returns: The mutated instance of query for easy chaining.

Tests/ParseSwiftTests/ParseLiveQueryTests.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,27 @@ class ParseLiveQueryTests: XCTestCase {
204204
XCTAssertEqual(decoded, expected)
205205
}
206206

207-
func testSubscribeMessageEncoding() throws {
207+
func testSubscribeMessageFieldsEncoding() throws {
208208
// swiftlint:disable:next line_length
209209
let expected = "{\"op\":\"subscribe\",\"query\":{\"className\":\"GameScore\",\"fields\":[\"points\"],\"where\":{\"points\":{\"$gt\":9}}},\"requestId\":1}"
210210
let query = GameScore.query("points" > 9)
211211
.fields(["points"])
212+
.select(["talk"])
213+
let message = SubscribeMessage(operation: .subscribe,
214+
requestId: RequestId(value: 1),
215+
query: query,
216+
additionalProperties: true)
217+
let encoded = try ParseCoding.jsonEncoder()
218+
.encode(message)
219+
let decoded = try XCTUnwrap(String(data: encoded, encoding: .utf8))
220+
XCTAssertEqual(decoded, expected)
221+
}
222+
223+
func testSubscribeMessageSelectEncoding() throws {
224+
// swiftlint:disable:next line_length
225+
let expected = "{\"op\":\"subscribe\",\"query\":{\"className\":\"GameScore\",\"fields\":[\"points\"],\"where\":{\"points\":{\"$gt\":9}}},\"requestId\":1}"
226+
let query = GameScore.query("points" > 9)
227+
.select(["points"])
212228
let message = SubscribeMessage(operation: .subscribe,
213229
requestId: RequestId(value: 1),
214230
query: query,

0 commit comments

Comments
 (0)