Skip to content

Commit 8abff8b

Browse files
committed
Small modifications to TUSBackground. Also verifies that uploading works.
1 parent 38ff9cf commit 8abff8b

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

Sources/TUSKit/TUSBackground.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ final class TUSBackground {
1717
private static let identifier = "io.tus.uploading"
1818

1919
private var currentTask: ScheduledTask?
20-
private let scheduler: BGTaskScheduler
2120
private let api: TUSAPI
2221
private let files: Files
2322

24-
init(scheduler: BGTaskScheduler, api: TUSAPI, files: Files) {
25-
self.scheduler = scheduler
23+
init(api: TUSAPI, files: Files) {
2624
self.api = api
2725
self.files = files
2826

@@ -33,13 +31,14 @@ final class TUSBackground {
3331
#if targetEnvironment(simulator)
3432
return
3533
#else
36-
scheduler.register(forTaskWithIdentifier: type(of: self).identifier, using: nil) { [weak self] bgTask in
34+
BGTaskScheduler.shared.register(forTaskWithIdentifier: type(of: self).identifier, using: nil) { [weak self] bgTask in
3735
guard let self = self else { return }
3836
guard let backgroundTask = bgTask as? BGProcessingTask else {
3937
return
4038
}
4139

4240
guard let tusTask = self.firstTask() else {
41+
backgroundTask.setTaskCompleted(success: true)
4342
return
4443
}
4544

@@ -50,17 +49,16 @@ final class TUSBackground {
5049
tusTask.cancel()
5150
}
5251

53-
tusTask.run { [weak self] result in
52+
tusTask.run { result in
5453
switch result {
5554
case .success:
5655
backgroundTask.setTaskCompleted(success: true)
5756
case .failure:
5857
backgroundTask.setTaskCompleted(success: false)
5958
}
60-
61-
guard let self = self else { return }
62-
self.scheduleSingleTask() // Try and schedule another task.
6359
}
60+
61+
self.scheduleSingleTask() // Try and schedule another task.
6462
}
6563
#endif
6664
}
@@ -81,7 +79,12 @@ final class TUSBackground {
8179

8280
let request = BGProcessingTaskRequest(identifier: type(of: self).identifier)
8381
request.requiresNetworkConnectivity = true
84-
try? scheduler.submit(request)
82+
do {
83+
try BGTaskScheduler.shared.submit(request)
84+
} catch {
85+
print("Could not schedule background task \(error)")
86+
}
87+
8588
}
8689

8790
/// Return first available task

Sources/TUSKit/TUSClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public final class TUSClient {
7474
#if os(iOS)
7575
@available(iOS 13.0, *)
7676
private lazy var backgroundClient: TUSBackground = {
77-
return TUSBackground(scheduler: BGTaskScheduler.shared, api: api, files: files)
77+
return TUSBackground(api: api, files: files)
7878
}()
7979
#endif
8080

TUSKitExample/TUSKitExample/SceneDelegate.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
2222
do {
2323
tusClient = try TUSClient(server: URL(string: "https://tusd.tusdemo.net/files")!, sessionIdentifier: "TUS DEMO", storageDirectory: URL(string: "/TUS")!)
2424
tusClient.delegate = self
25-
tusClient.start()
25+
let remainingUploads = tusClient.start()
26+
switch remainingUploads.count {
27+
case 0:
28+
print("No files to upload")
29+
case 1:
30+
print("Continuing uploading single file")
31+
case let nr:
32+
print("Continuing uploading \(nr) file(s)")
33+
}
2634

2735
// When starting, you can retrieve the locally stored uploads that are marked as failure, and handle those.
2836
// E.g. Maybe some uploads failed from a last session, or failed from a background upload.

0 commit comments

Comments
 (0)