Skip to content

Commit

Permalink
Execute more things on the session data task's queue
Browse files Browse the repository at this point in the history
  • Loading branch information
cysp committed May 2, 2019
1 parent 0cc3eb1 commit a79279b
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions Sources/DVR/SessionDataTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ final class SessionDataTask: URLSessionDataTask {
// Find interaction
if let interaction = session.cassette?.interactionForRequest(request) {
self.interaction = interaction
// Forward completion
if let completion = completion {
queue.async {
completion(interaction.responseData, interaction.response, nil)
}
}

queue.async { [weak self] in
guard let self = self else {
return
fatalError("[DVR] Something has gone horribly wrong.")
}

// Forward completion
if let completion = self.completion {
completion(interaction.responseData, interaction.response, nil)
}

self._state = .completed
Expand All @@ -82,20 +80,24 @@ final class SessionDataTask: URLSessionDataTask {
fatalError("[DVR] Failed to record because the task returned a nil response.")
}

guard let this = self else {
guard let self = self else {
fatalError("[DVR] Something has gone horribly wrong.")
}

// Still call the completion block so the user can chain requests while recording.
this.queue.async {
this.completion?(data, response, nil)
}
self.queue.async { [weak self] in
guard let self = self else {
fatalError("[DVR] Something has gone horribly wrong.")
}

this._state = .completed
self.completion?(data, response, nil)

// Create interaction
this.interaction = Interaction(request: this.request, response: response, responseData: data)
this.session.finishTask(this, interaction: this.interaction!, playback: false)
self._state = .completed

// Create interaction
self.interaction = Interaction(request: self.request, response: response, responseData: data)
self.session.finishTask(self, interaction: self.interaction!, playback: false)
}
})
task.resume()
}
Expand Down

0 comments on commit a79279b

Please sign in to comment.