Skip to content

Commit a6e949e

Browse files
authored
ParseLiveQuery checks all states of a websocket (#207)
* ParseLiveQuery checks all states of a websocket * Added completion handler to resumeTask * remove some changes to resumeTask * Update .codecov.yml * Remove extra receives * Add closeAll back * remove delegate if needed * Call open when error received * Call open when attempting to reconnect to add delay * Remove closeAll test. LiveQuery test a little flaky
1 parent 49e8083 commit a6e949e

File tree

6 files changed

+97
-40
lines changed

6 files changed

+97
-40
lines changed

.codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ coverage:
44
status:
55
patch:
66
default:
7-
target: auto
7+
target: 51
88
changes: false
99
project:
1010
default:

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# Parse-Swift Changelog
22

33
### main
4-
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.1...main)
4+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.2...main)
55
* _Contributing to this repo? Add info about your change here to be included in the next release_
66

7+
### 1.9.2
8+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.1...1.9.2)
9+
__Improvements__
10+
- ParseLiveQuery checks all states of a websocket and reacts as needed after an error ([#206](https://github.com/parse-community/Parse-Swift/pull/206)), thanks to [Corey Baker](https://github.com/cbaker6).
11+
712
### 1.9.1
813
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.0...1.9.1)
914

Sources/ParseSwift/LiveQuery/LiveQuerySocket.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ extension LiveQuerySocket {
5959
return
6060
}
6161
task.send(.string(encodedAsString)) { error in
62+
if error == nil {
63+
self.receive(task)
64+
}
6265
completion(error)
6366
}
64-
self.receive(task)
6567
}
6668
}
6769

@@ -74,9 +76,6 @@ extension LiveQuerySocket {
7476
return
7577
}
7678
task.send(.string(encodedAsString)) { error in
77-
if error == nil {
78-
self.receive(task)
79-
}
8079
completion(error)
8180
}
8281
}

Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ public final class ParseLiveQuery: NSObject {
200200
components.scheme = (components.scheme == "https" || components.scheme == "wss") ? "wss" : "ws"
201201
url = components.url
202202
self.task = URLSession.liveQuery.createTask(self.url)
203-
self.resumeTask()
204-
203+
self.resumeTask { _ in }
205204
if isDefault {
206205
Self.setDefault(self)
207206
}
@@ -227,13 +226,25 @@ extension ParseLiveQuery {
227226
return Int.random(in: 0 ..< Int(truncating: min))
228227
}
229228

230-
func resumeTask() {
229+
func resumeTask(completion: @escaping (Error?) -> Void) {
231230
synchronizationQueue.sync {
232-
if self.task.state == .suspended {
233-
self.task.resume()
231+
switch self.task.state {
232+
case .suspended:
233+
task.resume()
234+
URLSession.liveQuery.delegates.removeValue(forKey: self.task)
235+
URLSession.liveQuery.delegates[self.task] = self
236+
completion(nil)
237+
case .completed, .canceling:
238+
URLSession.liveQuery.delegates.removeValue(forKey: self.task)
239+
task = URLSession.liveQuery.createTask(self.url)
240+
task.resume()
241+
URLSession.liveQuery.delegates[self.task] = self
242+
completion(nil)
243+
case .running:
244+
open(isUserWantsToConnect: false, completion: completion)
245+
@unknown default:
246+
break
234247
}
235-
URLSession.liveQuery.receive(self.task)
236-
URLSession.liveQuery.delegates[self.task] = self
237248
}
238249
}
239250

@@ -322,8 +333,8 @@ extension ParseLiveQuery: LiveQuerySocketDelegate {
322333
}
323334
self.isSocketEstablished = false
324335
if !self.isDisconnectedByUser {
325-
//Try to reconnect
326-
self.resumeTask()
336+
// Try to reconnect
337+
self.open(isUserWantsToConnect: false) { _ in }
327338
}
328339
}
329340
}
@@ -337,7 +348,7 @@ extension ParseLiveQuery: LiveQuerySocketDelegate {
337348
if self.isConnected {
338349
self.close(useDedicatedQueue: true)
339350
//Try to reconnect
340-
self.resumeTask()
351+
self.resumeTask { _ in }
341352
}
342353
}
343354
return
@@ -484,9 +495,20 @@ extension ParseLiveQuery: LiveQuerySocketDelegate {
484495
Max attempts (\(ParseLiveQueryConstants.maxConnectionAttempts) reached.
485496
Not attempting to connect to LiveQuery server anymore.
486497
""")
487-
self.receiveDelegate?.received(parseError)
498+
notificationQueue.async {
499+
self.receiveDelegate?.received(parseError)
500+
}
501+
}
502+
isSocketEstablished = false
503+
open(isUserWantsToConnect: false) { error in
504+
guard let error = error else {
505+
// Resumed task successfully
506+
return
507+
}
508+
self.notificationQueue.async {
509+
self.receiveDelegate?.received(error)
510+
}
488511
}
489-
self.open(isUserWantsToConnect: false) { _ in }
490512
} else {
491513
notificationQueue.async {
492514
self.receiveDelegate?.received(error)
@@ -558,12 +580,12 @@ extension ParseLiveQuery {
558580
self.synchronizationQueue
559581
.asyncAfter(deadline: .now() + DispatchTimeInterval
560582
.seconds(reconnectInterval)) {
561-
self.resumeTask()
562-
self.attempts += 1
563-
let error = ParseError(code: .unknownError,
564-
// swiftlint:disable:next line_length
565-
message: "ParseLiveQuery Error: attempted to open socket \(self.attempts) time(s)")
566-
completion(error)
583+
self.attempts += 1
584+
self.resumeTask { _ in }
585+
let error = ParseError(code: .unknownError,
586+
// swiftlint:disable:next line_length
587+
message: "ParseLiveQuery Error: attempted to open socket \(self.attempts) time(s)")
588+
completion(error)
567589
}
568590
}
569591
}

Sources/ParseSwift/ParseConstants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
enum ParseConstants {
1212
static let sdk = "swift"
13-
static let version = "1.9.1"
13+
static let version = "1.9.2"
1414
static let fileManagementDirectory = "parse/"
1515
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
1616
static let fileManagementLibraryDirectory = "Library/"

0 commit comments

Comments
 (0)