Skip to content

Commit

Permalink
feat: Making sure BackgroundTasksService is aware of activity termina…
Browse files Browse the repository at this point in the history
…tion and can stop gracefully
  • Loading branch information
adrien-coye committed Feb 6, 2024
1 parent f5d5fdc commit 273dd1f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
30 changes: 29 additions & 1 deletion kDriveCore/Services/BackgroundTasksService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,53 @@ struct BackgroundTasksService: BackgroundTasksServiceable {
}

func handleBackgroundRefresh(completion: @escaping (Bool) -> Void) {
let expiringActivity = ExpiringActivity(id: UUID().uuidString, delegate: nil)
expiringActivity.start()

Log.backgroundTaskScheduling("handleBackgroundRefresh")
// User installed the app but never logged in
if accountManager.accounts.isEmpty {
if expiringActivity.shouldTerminate || accountManager.accounts.isEmpty {
Log.backgroundTaskScheduling("Notified activity should terminate", level: .error)
completion(false)
expiringActivity.endAll()
return
}

Log.backgroundTaskScheduling("Enqueue new pictures")
photoUploader.scheduleNewPicturesForUpload()

guard expiringActivity.shouldTerminate == false else {
Log.backgroundTaskScheduling("Notified activity should terminate", level: .error)
completion(false)
expiringActivity.endAll()
return
}

Log.backgroundTaskScheduling("Clean errors for all uploads")
uploadQueue.cleanNetworkAndLocalErrorsForAllOperations()

guard expiringActivity.shouldTerminate == false else {
Log.backgroundTaskScheduling("Notified activity should terminate", level: .error)
completion(false)
expiringActivity.endAll()
return
}

Log.backgroundTaskScheduling("Reload operations in queue")
uploadQueue.rebuildUploadQueueFromObjectsInRealm()

guard expiringActivity.shouldTerminate == false else {
Log.backgroundTaskScheduling("Notified activity should terminate", level: .error)
completion(false)
expiringActivity.endAll()
return
}

Log.backgroundTaskScheduling("waitForCompletion")
uploadQueue.waitForCompletion {
Log.backgroundTaskScheduling("Background activity ended with success")
completion(true)
expiringActivity.endAll()
}
}

Expand Down
6 changes: 6 additions & 0 deletions kDriveCore/Utils/ExpiringActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public protocol ExpiringActivityable {

/// Terminate all the expiring activities
func endAll()

/// True if the system asked to stop the background activity
var shouldTerminate: Bool { get }
}

public final class ExpiringActivity: ExpiringActivityable {
Expand All @@ -59,6 +62,8 @@ public final class ExpiringActivity: ExpiringActivityable {

let id: String

public var shouldTerminate = false

weak var delegate: ExpiringActivityDelegate?

// MARK: Lifecycle
Expand Down Expand Up @@ -104,6 +109,7 @@ public final class ExpiringActivity: ExpiringActivityable {
}

if shouldTerminate {
self.shouldTerminate = true
delegate?.backgroundActivityExpiring()
}

Expand Down

0 comments on commit 273dd1f

Please sign in to comment.