-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from uploadcare/develop
0.3.1
- Loading branch information
Showing
5 changed files
with
559 additions
and
476 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// BackgroundSessionManager.swift | ||
// | ||
// | ||
// Created by Sergey Armodin on 03.12.2020. | ||
// Copyright © 2020 Uploadcare, Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
class BackgroundSessionManager: NSObject { | ||
static let instance = BackgroundSessionManager() | ||
|
||
lazy var session: URLSession = { | ||
let bundle = Bundle.main.bundleIdentifier ?? "" | ||
let config = URLSessionConfiguration.background(withIdentifier: "\(bundle).com.uploadcare.backgroundUrlSession") | ||
config.isDiscretionary = false | ||
|
||
// TODO: add a public settings for that | ||
#if !os(macOS) | ||
config.sessionSendsLaunchEvents = true | ||
#endif | ||
|
||
config.waitsForConnectivity = true | ||
return URLSession(configuration: config, delegate: self, delegateQueue: nil) | ||
}() | ||
weak var sessionDelegate: URLSessionDelegate? | ||
} | ||
|
||
// MARK: - URLSessionTaskDelegate proxy | ||
extension BackgroundSessionManager: URLSessionDataDelegate { | ||
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) { | ||
(sessionDelegate as? URLSessionDataDelegate)?.urlSession?(session, dataTask: dataTask, didReceive: response, completionHandler: completionHandler) | ||
} | ||
} | ||
|
||
// MARK: - URLSessionTaskDelegate proxy | ||
extension BackgroundSessionManager: URLSessionTaskDelegate { | ||
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { | ||
(sessionDelegate as? URLSessionTaskDelegate)?.urlSession?(session, task: task, didCompleteWithError: error) | ||
} | ||
|
||
public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) { | ||
(sessionDelegate as? URLSessionTaskDelegate)?.urlSession?(session, task: task, didSendBodyData: bytesSent, totalBytesSent: totalBytesSent, totalBytesExpectedToSend: totalBytesExpectedToSend) | ||
} | ||
|
||
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { | ||
(sessionDelegate as? URLSessionDataDelegate)?.urlSession?(session, dataTask: dataTask, didReceive: data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.