Skip to content

Commit 901e79b

Browse files
committed
Track data usage in BackgroundSyncManager
Records bytes transferred during background sync operations.
1 parent 50f9039 commit 901e79b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Modules/Server/Sources/PocketCastsServer/Public/Sync/BackgroundSyncManager+URLSession.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@ import PocketCastsDataModel
33
import PocketCastsUtils
44

55
extension BackgroundSyncManager: URLSessionDelegate, URLSessionDownloadDelegate {
6+
public func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
7+
let bytesReceived = task.countOfBytesReceived
8+
let bytesSent = task.countOfBytesSent
9+
let isCellular = metrics.transactionMetrics.last?.isCellular ?? false
10+
11+
if bytesReceived > 0 || bytesSent > 0 {
12+
let connectionType: CellularDataUsageManager.ConnectionType = isCellular ? .cellular : .wifi
13+
14+
DataManager.sharedManager.cellularDataUsageManager.add(
15+
bytesDownloaded: bytesReceived,
16+
bytesUploaded: bytesSent,
17+
operationType: .sync,
18+
connectionType: connectionType,
19+
sessionType: .background
20+
)
21+
}
22+
}
23+
624
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
725
// one of our 3 tasks has finished, but since they can come back in any order we need to decide whether to process this now, or when another one return
826
// we also extract the data here, because it will be deleted when this method exits, and some of the things we call run on another thread

0 commit comments

Comments
 (0)