From 0cc3eb1b2b72292585e2a7049adc0629988c7352 Mon Sep 17 00:00:00 2001 From: Scott Talbot Date: Thu, 2 May 2019 19:24:21 +1000 Subject: [PATCH] Implement SessionTask.State --- Sources/DVR/SessionDataTask.swift | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Sources/DVR/SessionDataTask.swift b/Sources/DVR/SessionDataTask.swift index 8ac1a3a..d7390d5 100644 --- a/Sources/DVR/SessionDataTask.swift +++ b/Sources/DVR/SessionDataTask.swift @@ -31,6 +31,11 @@ final class SessionDataTask: URLSessionDataTask { // MARK: - URLSessionTask + var _state: URLSessionTask.State? + override var state: URLSessionTask.State { + return _state ?? .suspended + } + override func cancel() { // Don't do anything } @@ -38,6 +43,8 @@ final class SessionDataTask: URLSessionDataTask { override func resume() { let cassette = session.cassette + _state = .running + // Find interaction if let interaction = session.cassette?.interactionForRequest(request) { self.interaction = interaction @@ -47,7 +54,15 @@ final class SessionDataTask: URLSessionDataTask { completion(interaction.responseData, interaction.response, nil) } } - session.finishTask(self, interaction: interaction, playback: true) + + queue.async { [weak self] in + guard let self = self else { + return + } + + self._state = .completed + self.session.finishTask(self, interaction: interaction, playback: true) + } return } @@ -76,6 +91,8 @@ final class SessionDataTask: URLSessionDataTask { this.completion?(data, response, nil) } + this._state = .completed + // Create interaction this.interaction = Interaction(request: this.request, response: response, responseData: data) this.session.finishTask(this, interaction: this.interaction!, playback: false)