Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to reconnect to LiveQuery server after error #204

Merged
merged 7 commits into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ coverage:
status:
patch:
default:
target: auto
target: 22
changes: false
project:
default:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
__Improvements__
- Clear caching when a user logs out ([#198](https://github.com/parse-community/Parse-Swift/pull/198)), thanks to [Corey Baker](https://github.com/cbaker6).
- Close all LiveQuery connections when a user logs out ([#199](https://github.com/parse-community/Parse-Swift/pull/199)), thanks to [Corey Baker](https://github.com/cbaker6).
- ParseLiveQuery attempts to reconnect upon disconnection error ([#204](https://github.com/parse-community/Parse-Swift/pull/204)), thanks to [Corey Baker](https://github.com/cbaker6).

__Fixes__
- Fix Facebook and Twitter login setting incorrect keys ([#202](https://github.com/parse-community/Parse-Swift/pull/202)), thanks to [Daniel Blyth](https://github.com/dblythy).
Expand Down
14 changes: 9 additions & 5 deletions Sources/ParseSwift/LiveQuery/LiveQuerySocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,22 @@ extension LiveQuerySocket {
task.receive { result in
switch result {
case .success(.string(let message)):
guard let data = message.data(using: .utf8) else {
return
if let data = message.data(using: .utf8) {
self.delegates[task]?.received(data)
} else {
let parseError = ParseError(code: .unknownError,
message: "Couldn't encode LiveQuery string as data")
self.delegates[task]?.receivedError(parseError)
}
self.delegates[task]?.received(data)
self.receive(task)
case .success(.data(let data)):
self.delegates[task]?.receivedUnsupported(data, socketMessage: nil)
self.receive(task)
case .success(let message):
self.delegates[task]?.receivedUnsupported(nil, socketMessage: message)
self.receive(task)
case .failure(let error):
let parseError = ParseError(code: .unknownError, message: error.localizedDescription)
self.delegates[task]?.receivedError(parseError)
self.delegates[task]?.receivedError(error)
}
}
}
Expand Down
24 changes: 21 additions & 3 deletions Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,27 @@ extension ParseLiveQuery: LiveQuerySocketDelegate {
}
}

func receivedError(_ error: ParseError) {
notificationQueue.async {
self.receiveDelegate?.received(error)
func receivedError(_ error: Error) {
guard let posixError = error as? POSIXError else {
notificationQueue.async {
self.receiveDelegate?.received(error)
}
return
}
if posixError.code == .ENOTCONN {
if attempts + 1 >= ParseLiveQueryConstants.maxConnectionAttempts + 1 {
let parseError = ParseError(code: .unknownError,
message: """
Max attempts (\(ParseLiveQueryConstants.maxConnectionAttempts) reached.
Not attempting to connect to LiveQuery server anymore.
""")
self.receiveDelegate?.received(parseError)
}
self.open(isUserWantsToConnect: false) { _ in }
} else {
notificationQueue.async {
self.receiveDelegate?.received(error)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protocol LiveQuerySocketDelegate: AnyObject {
closeCode: URLSessionWebSocketTask.CloseCode?,
reason: Data?)
func close(useDedicatedQueue: Bool)
func receivedError(_ error: ParseError)
func receivedError(_ error: Error)
func receivedUnsupported(_ data: Data?, socketMessage: URLSessionWebSocketTask.Message?)
func received(challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public protocol ParseLiveQueryDelegate: AnyObject {
Receive errors from the ParseLiveQuery task/connection.
- parameter error: An error from the session task.
*/
func received(_ error: ParseError)
func received(_ error: Error)

/**
Receive unsupported data from the ParseLiveQuery task/connection.
Expand Down Expand Up @@ -68,13 +68,13 @@ public protocol ParseLiveQueryDelegate: AnyObject {
}

@available(macOS 10.15, iOS 13.0, macCatalyst 13.0, watchOS 6.0, tvOS 13.0, *)
extension ParseLiveQueryDelegate {
public extension ParseLiveQueryDelegate {
func received(_ challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition,
URLCredential?) -> Void) {
completionHandler(.performDefaultHandling, nil)
}
func received(_ error: ParseError) { }
func received(_ error: Error) { }
func receivedUnsupported(_ data: Data?, socketMessage: URLSessionWebSocketTask.Message?) { }
func received(_ metrics: URLSessionTaskTransactionMetrics) { }
func closedSocket(_ code: URLSessionWebSocketTask.CloseCode?, reason: Data?) { }
Expand Down
6 changes: 4 additions & 2 deletions Tests/ParseSwiftTests/ParseLiveQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ class ParseLiveQueryTests: XCTestCase {
var error: ParseError?
var code: URLSessionWebSocketTask.CloseCode?
var reason: Data?
func received(_ error: ParseError) {
self.error = error
func received(_ error: Error) {
if let error = error as? ParseError {
self.error = error
}
}
func closedSocket(_ code: URLSessionWebSocketTask.CloseCode?, reason: Data?) {
self.code = code
Expand Down