Skip to content

Commit 69dce16

Browse files
authored
refactor: add defaultClient to LiveQuery to replace getDefault() (#342)
* refactor: add defaultClient to LiveQuery to replace getDefault() * nit * change log
1 parent c8252bc commit 69dce16

File tree

6 files changed

+74
-48
lines changed

6 files changed

+74
-48
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.1.0...main)
66
* _Contributing to this repo? Add info about your change here to be included in the next release_
77

8+
__Improvements__
9+
- Add clientDefault static property to ParseLiveQuery which replaces the getDefault() method. getDefault() is still avaiable, but will be deprecated in ParseSwift 5.0.0 so it is recommended to switch to clientDefault ([#342](https://github.com/parse-community/Parse-Swift/pull/342)), thanks to [Corey Baker](https://github.com/cbaker6).
10+
811
### 4.1.0
912
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/4.0.1...4.1.0)
1013

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class LiveQueryDelegate: ParseLiveQueryDelegate {
6868

6969
//: Set the delegate.
7070
let delegate = LiveQueryDelegate()
71-
if let socket = ParseLiveQuery.getDefault() {
71+
if let socket = ParseLiveQuery.defaultClient {
7272
socket.receiveDelegate = delegate
7373
}
7474

Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ Not attempting to open ParseLiveQuery socket anymore
223223
}
224224
}
225225

226+
// MARK: Client Intents
226227
extension ParseLiveQuery {
227228

228229
/// Current LiveQuery client.
@@ -265,20 +266,29 @@ extension ParseLiveQuery {
265266
}
266267
}
267268

269+
/// The default `ParseLiveQuery` client for all LiveQuery connections.
270+
class public var defaultClient: ParseLiveQuery? {
271+
Self.client
272+
}
273+
268274
/// Set a specific ParseLiveQuery client to be the default for all `ParseLiveQuery` connections.
269275
/// - parameter client: The client to set as the default.
270276
class public func setDefault(_ client: ParseLiveQuery) {
271-
ParseLiveQuery.client = nil
272-
ParseLiveQuery.client = client
277+
Self.client = nil
278+
Self.client = client
273279
}
274280

275281
/// Get the default `ParseLiveQuery` client for all LiveQuery connections.
282+
/// - returns: The default `ParseLiveQuery` client.
283+
/// - warning: This will be deprecated in ParseSwift 5.0.0 in favor of `defaultClient`.
276284
class public func getDefault() -> ParseLiveQuery? {
277-
ParseLiveQuery.client
285+
Self.defaultClient
278286
}
279287

280288
/// Check if a query has an active subscription on this `ParseLiveQuery` client.
281289
/// - parameter query: Query to verify.
290+
/// - returns: **true** if subscribed. **false** otherwise.
291+
/// - throws: An error of type `ParseError`.
282292
public func isSubscribed<T: ParseObject>(_ query: Query<T>) throws -> Bool {
283293
let queryData = try ParseCoding.jsonEncoder().encode(query)
284294
return subscriptions.contains(where: { (_, value) -> Bool in
@@ -292,6 +302,8 @@ extension ParseLiveQuery {
292302

293303
/// Check if a query has a pending subscription on this `ParseLiveQuery` client.
294304
/// - parameter query: Query to verify.
305+
/// - returns: **true** if query is a pending subscription. **false** otherwise.
306+
/// - throws: An error of type `ParseError`.
295307
public func isPendingSubscription<T: ParseObject>(_ query: Query<T>) throws -> Bool {
296308
let queryData = try ParseCoding.jsonEncoder().encode(query)
297309
return pendingSubscriptions.contains(where: { (_, value) -> Bool in
@@ -305,6 +317,7 @@ extension ParseLiveQuery {
305317

306318
/// Remove a pending subscription on this `ParseLiveQuery` client.
307319
/// - parameter query: Query to remove.
320+
/// - throws: An error of type `ParseError`.
308321
public func removePendingSubscription<T: ParseObject>(_ query: Query<T>) throws {
309322
let queryData = try ParseCoding.jsonEncoder().encode(query)
310323
pendingSubscriptions.removeAll(where: { (_, value) -> Bool in
@@ -851,7 +864,8 @@ public extension Query {
851864
as the subscription can be used as a SwiftUI publisher. Meaning it can serve
852865
indepedently as a ViewModel in MVVM.
853866
- parameter client: A specific client.
854-
- returns: The subscription that has just been registered
867+
- returns: The subscription that has just been registered.
868+
- throws: An error of type `ParseError`.
855869
*/
856870
func subscribe(_ client: ParseLiveQuery) throws -> Subscription<ResultType> {
857871
try client.subscribe(Subscription(query: self))
@@ -862,6 +876,7 @@ public extension Query {
862876
Registers a query for live updates, using a custom subscription handler.
863877
- parameter handler: A custom subscription handler.
864878
- returns: Your subscription handler, for easy chaining.
879+
- throws: An error of type `ParseError`.
865880
*/
866881
static func subscribe<T: QuerySubscribable>(_ handler: T) throws -> T {
867882
if let client = ParseLiveQuery.client {
@@ -876,6 +891,7 @@ public extension Query {
876891
- parameter handler: A custom subscription handler.
877892
- parameter client: A specific client.
878893
- returns: Your subscription handler, for easy chaining.
894+
- throws: An error of type `ParseError`.
879895
*/
880896
static func subscribe<T: QuerySubscribable>(_ handler: T, client: ParseLiveQuery) throws -> T {
881897
try client.subscribe(handler)
@@ -894,6 +910,7 @@ public extension Query {
894910
and a specific `ParseLiveQuery` client.
895911
- parameter client: A specific client.
896912
- returns: The subscription that has just been registered.
913+
- throws: An error of type `ParseError`.
897914
*/
898915
func subscribeCallback(_ client: ParseLiveQuery) throws -> SubscriptionCallback<ResultType> {
899916
try client.subscribe(SubscriptionCallback(query: self))
@@ -905,6 +922,7 @@ public extension Query {
905922
/**
906923
Unsubscribes all current subscriptions for a given query on the default
907924
`ParseLiveQuery` client.
925+
- throws: An error of type `ParseError`.
908926
*/
909927
func unsubscribe() throws {
910928
try ParseLiveQuery.client?.unsubscribe(self)
@@ -914,6 +932,7 @@ public extension Query {
914932
Unsubscribes all current subscriptions for a given query on a specific
915933
`ParseLiveQuery` client.
916934
- parameter client: A specific client.
935+
- throws: An error of type `ParseError`.
917936
*/
918937
func unsubscribe(client: ParseLiveQuery) throws {
919938
try client.unsubscribe(self)
@@ -923,6 +942,7 @@ public extension Query {
923942
Unsubscribes from a specific query-handler on the default
924943
`ParseLiveQuery` client.
925944
- parameter handler: The specific handler to unsubscribe from.
945+
- throws: An error of type `ParseError`.
926946
*/
927947
func unsubscribe<T: QuerySubscribable>(_ handler: T) throws {
928948
try ParseLiveQuery.client?.unsubscribe(handler)
@@ -933,6 +953,7 @@ public extension Query {
933953
`ParseLiveQuery` client.
934954
- parameter handler: The specific handler to unsubscribe from.
935955
- parameter client: A specific client.
956+
- throws: An error of type `ParseError`.
936957
*/
937958
func unsubscribe<T: QuerySubscribable>(_ handler: T, client: ParseLiveQuery) throws {
938959
try client.unsubscribe(handler)
@@ -945,6 +966,7 @@ public extension Query {
945966
Updates an existing subscription with a new query on the default `ParseLiveQuery` client.
946967
Upon completing the registration, the subscribe handler will be called with the new query.
947968
- parameter handler: The specific handler to update.
969+
- throws: An error of type `ParseError`.
948970
*/
949971
func update<T: QuerySubscribable>(_ handler: T) throws {
950972
try ParseLiveQuery.client?.update(handler)
@@ -955,6 +977,7 @@ public extension Query {
955977
Upon completing the registration, the subscribe handler will be called with the new query.
956978
- parameter handler: The specific handler to update.
957979
- parameter client: A specific client.
980+
- throws: An error of type `ParseError`.
958981
*/
959982
func update<T: QuerySubscribable>(_ handler: T, client: ParseLiveQuery) throws {
960983
try client.update(handler)

Tests/ParseSwiftTests/ParseLiveQueryAsyncTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ParseLiveQueryAsyncTests: XCTestCase { // swiftlint:disable:this type_body
4141

4242
@MainActor
4343
func testOpen() async throws {
44-
guard let client = ParseLiveQuery.getDefault() else {
44+
guard let client = ParseLiveQuery.defaultClient else {
4545
XCTFail("Should be able to get client")
4646
return
4747
}
@@ -57,7 +57,7 @@ class ParseLiveQueryAsyncTests: XCTestCase { // swiftlint:disable:this type_body
5757

5858
@MainActor
5959
func testPingSocketNotEstablished() async throws {
60-
guard let client = ParseLiveQuery.getDefault() else {
60+
guard let client = ParseLiveQuery.defaultClient else {
6161
XCTFail("Should be able to get client")
6262
return
6363
}
@@ -80,7 +80,7 @@ class ParseLiveQueryAsyncTests: XCTestCase { // swiftlint:disable:this type_body
8080

8181
@MainActor
8282
func testPing() async throws {
83-
guard let client = ParseLiveQuery.getDefault() else {
83+
guard let client = ParseLiveQuery.defaultClient else {
8484
XCTFail("Should be able to get client")
8585
return
8686
}

Tests/ParseSwiftTests/ParseLiveQueryCombineTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ParseLiveQueryCombineTests: XCTestCase {
4141
}
4242

4343
func testOpen() throws {
44-
guard let client = ParseLiveQuery.getDefault() else {
44+
guard let client = ParseLiveQuery.defaultClient else {
4545
XCTFail("Should be able to get client")
4646
return
4747
}
@@ -69,7 +69,7 @@ class ParseLiveQueryCombineTests: XCTestCase {
6969
}
7070

7171
func testPingSocketNotEstablished() throws {
72-
guard let client = ParseLiveQuery.getDefault() else {
72+
guard let client = ParseLiveQuery.defaultClient else {
7373
XCTFail("Should be able to get client")
7474
return
7575
}
@@ -104,7 +104,7 @@ class ParseLiveQueryCombineTests: XCTestCase {
104104
}
105105

106106
func testPing() throws {
107-
guard let client = ParseLiveQuery.getDefault() else {
107+
guard let client = ParseLiveQuery.defaultClient else {
108108
XCTFail("Should be able to get client")
109109
return
110110
}

0 commit comments

Comments
 (0)