Skip to content

Commit

Permalink
Merge pull request #44 from 3sidedcube/fix/background_download_memory…
Browse files Browse the repository at this point in the history
…_limits

Adds option to stop background download controller reading to disk
  • Loading branch information
simonmitchell authored Apr 8, 2020
2 parents 8f128aa + d36e720 commit 742e52b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Thunder Request

[![Build Status](https://travis-ci.org/3sidedcube/ThunderRequest.svg)](https://travis-ci.org/3sidedcube/ThunderRequest) [![Swift 5.1](http://img.shields.io/badge/swift-5.1-brightgreen.svg)](https://swift.org/blog/swift-5-1-released/) [![Apache 2](https://img.shields.io/badge/license-Apache%202-brightgreen.svg)](LICENSE.md)
[![Build Status](https://travis-ci.org/3sidedcube/ThunderRequest.svg)](https://travis-ci.org/3sidedcube/ThunderRequest) [![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![Swift 5.2](http://img.shields.io/badge/swift-5.2-brightgreen.svg)](https://swift.org/blog/swift-5-2-released/) [![Apache 2](https://img.shields.io/badge/license-Apache%202-brightgreen.svg)](LICENSE.md)

Thunder Request is a Framework used to simplify making http and https web requests.

Expand Down
16 changes: 13 additions & 3 deletions ThunderRequest/BackgroundSessionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ public class BackgroundRequestController: NSObject, URLSessionDelegate, URLSessi

private var urlSession: URLSession?

private let readData: Bool

/// Creates a new request controller with a background session configuration identifier passed by the OS.
///
/// - Parameters:
/// - identifier: The identifier to re-create the `URLSessionConfiguration` using.
/// - responseHandler: A closure called with the response to each background request.
/// - finishHandler: A closure called when all background events have finished.
/// - finishedHandler: A closure called when all background events have finished.
/// - queue: The operation queue to call back on.
public init(identifier: String, responseHandler: ResponseHandler?, finishedHandler: FinishHandler?, queue: OperationQueue? = nil) {
/// - readDataAutomatically: Setting this to false allows you to stop the downloaded file's data being read from disk.
/// Reading from disk can cause problems with large background downloads as the background Daemon has a ~40mb memory limit
/// before being killed by the OS!
public init(identifier: String, responseHandler: ResponseHandler?, finishedHandler: FinishHandler?, readDataAutomatically: Bool = true, queue: OperationQueue? = nil) {

readData = readDataAutomatically
self.responseHandler = responseHandler
self.finishedHandler = finishedHandler
sessionConfiguration = URLSessionConfiguration.background(withIdentifier: identifier)
Expand All @@ -56,7 +62,11 @@ public class BackgroundRequestController: NSObject, URLSessionDelegate, URLSessi
responseHandler?(downloadTask, nil, nil)
return
}
let response = RequestResponse(response: taskResponse, data: try? Data(contentsOf: location))
var data: Data?
if readData {
data = try? Data(contentsOf: location)
}
let response = RequestResponse(response: taskResponse, data: data, fileURL: location)
responseHandler?(downloadTask, response, nil)
}

Expand Down
2 changes: 1 addition & 1 deletion ThunderRequest/RequestController+Callbacks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension RequestController {

var requestResponse: RequestResponse?
if let urlResponse = response {
requestResponse = RequestResponse(response: urlResponse, data: nil)
requestResponse = RequestResponse(response: urlResponse, data: nil, fileURL: fileURL)
}

transferCompletionHandlers[taskIdentifier]?(requestResponse, fileURL, error)
Expand Down
2 changes: 1 addition & 1 deletion ThunderRequest/RequestController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ open class RequestController {
RequestController.hideApplicationActivityIndicator()
var returnResponse: RequestResponse?
if let requestResponse = response.response {
returnResponse = RequestResponse(response: requestResponse, data: nil)
returnResponse = RequestResponse(response: requestResponse, data: nil, fileURL: response.downloadURL)
}
completion?(returnResponse, response.downloadURL, error)

Expand Down
9 changes: 7 additions & 2 deletions ThunderRequest/RequestResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class RequestResponse {
return httpResponse?.allHeaderFields
}

/// File url the response's data was saved to. This is only present for file downloads and is useful
/// to get around the `40mb` memory limit which the Apple background service imposes on background download daemon
public let fileURL: URL?

/// Raw data returned from the server
public let data: Data?

Expand All @@ -31,10 +35,11 @@ public class RequestResponse {
/// Initialises a new request response from a given `URLResponse` and `Data`
/// - Parameter response: The response to populate properties with
/// - Parameter data: The data that was returned with the response
public init(response: URLResponse, data: Data?) {
/// - Parameter fileURL: The file url that the responses download was saved to (Only present for download tasks!)
public init(response: URLResponse, data: Data?, fileURL: URL? = nil) {
httpResponse = response as? HTTPURLResponse
self.data = data
self.fileURL = fileURL
}

/// The status of the HTTP request as an enum
Expand Down

0 comments on commit 742e52b

Please sign in to comment.