Skip to content

Commit

Permalink
fix: mark uploads as background tasks to let them continue if the app…
Browse files Browse the repository at this point in the history
… enters the background (#78)

* fix: mark uploads as background tasks to let them continue if the app enters the background

* fix: use URLSessionConfiguration.ephemeral instead of URLSession.shared session

* fix: updated session configuration
  • Loading branch information
falconandy authored Sep 12, 2023
1 parent 85dad09 commit 80b465e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Sources/Amplitude/Plugins/Vendors/AppUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ import Foundation
override var requiredPlugin: Plugin {
return IOSLifecycleMonitor()
}

override func beginBackgroundTask() -> BackgroundTaskCompletionCallback? {
var id: UIBackgroundTaskIdentifier = .invalid
let callback = { () in
UIApplication.shared.endBackgroundTask(id)
id = .invalid
}
id = UIApplication.shared.beginBackgroundTask(withName: "amplitude", expirationHandler: callback)
return callback
}
}
#endif

Expand Down
6 changes: 6 additions & 0 deletions Sources/Amplitude/Plugins/Vendors/VendorSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// Created by Hao Yu on 11/11/22.
//

internal typealias BackgroundTaskCompletionCallback = () -> Void

internal class VendorSystem {
var manufacturer: String {
return "unknown"
Expand Down Expand Up @@ -45,4 +47,8 @@ internal class VendorSystem {
var requiredPlugin: Plugin? {
return nil
}

func beginBackgroundTask() -> BackgroundTaskCompletionCallback? {
return nil
}
}
13 changes: 9 additions & 4 deletions Sources/Amplitude/Utilities/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import Foundation

class HttpClient {
let configuration: Configuration
internal var session: URLSession
internal let session: URLSession

init(configuration: Configuration) {
self.configuration = configuration
// shared instance has limitations but think we are not affected
// https://developer.apple.com/documentation/foundation/urlsession/1409000-shared
self.session = URLSession.shared

let sessionConfiguration = URLSessionConfiguration.default
sessionConfiguration.httpMaximumConnectionsPerHost = 2
sessionConfiguration.urlCache = nil
self.session = URLSession(configuration: sessionConfiguration, delegate: nil, delegateQueue: nil)
}

func upload(events: String, completion: @escaping (_ result: Result<Int, Error>) -> Void) -> URLSessionDataTask? {
var sessionTask: URLSessionDataTask?
let backgroundTaskCompletion = VendorSystem.current.beginBackgroundTask()
do {
let request = try getRequest()
let requestData = getRequestData(events: events)
Expand All @@ -35,10 +38,12 @@ class HttpClient {
completion(.failure(Exception.httpError(code: httpResponse.statusCode, data: data)))
}
}
backgroundTaskCompletion?()
}
sessionTask!.resume()
} catch {
completion(.failure(Exception.httpError(code: 500, data: nil)))
backgroundTaskCompletion?()
}
return sessionTask
}
Expand Down

0 comments on commit 80b465e

Please sign in to comment.