Skip to content

Commit d072677

Browse files
committed
Only submitBGContinuedProcessingTask when the app goes to background
1 parent e35fbcd commit d072677

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

WordPress/Classes/Services/MediaCoordinator.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,20 @@ class MediaCoordinator: NSObject {
453453
WPAppAnalytics.track(event, properties: properties, blog: media.blog)
454454
}
455455

456+
@MainActor
457+
func scheduleUploadingTasks() async {
458+
let coordinator = mediaLibraryProgressCoordinator
459+
460+
for (mediaID, progress) in coordinator.mediaInProgress {
461+
guard let media = coordinator.media(withIdentifier: mediaID) else {
462+
continue
463+
}
464+
if media.remoteStatus == .pushing || media.remoteStatus == .processing {
465+
await mediaUploadBackgroundTaskScheduler()?.scheduleTask(for: TaggedManagedObjectID(media), progress: progress)
466+
}
467+
}
468+
}
469+
456470
// MARK: - Progress
457471

458472
/// - returns: The current progress for the specified media object.

WordPress/Classes/System/WordPressAppDelegate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ public class WordPressAppDelegate: UIResponder, UIApplicationDelegate {
193193

194194
public func applicationWillResignActive(_ application: UIApplication) {
195195
DDLogInfo("\(self) \(#function)")
196+
Task {
197+
await MediaCoordinator.shared.scheduleUploadingTasks()
198+
}
196199
}
197200

198201
public func applicationDidBecomeActive(_ application: UIApplication) {

WordPress/Classes/ViewRelated/Aztec/Media/MediaProgressCoordinator.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,21 @@ public class MediaProgressCoordinator: NSObject {
7272
progress.setUserInfoObject(media, forKey: .mediaObject)
7373
mediaGlobalProgress?.addChild(progress, withPendingUnitCount: 1)
7474
mediaInProgress[mediaID] = progress
75+
}
76+
77+
/// Utilize BGContinuedProcessingTask to show upload progress and extend the allowed background time for the upload.
78+
/// Note: This function needs to be called before the app goes to the background.
79+
@MainActor
80+
func submitBackgroundUploadTask() async {
81+
guard let scheduler = mediaUploadBackgroundTaskScheduler() else { return }
7582

76-
let objectID = TaggedManagedObjectID(media)
77-
Task {
78-
await mediaUploadBackgroundTaskScheduler()?.scheduleTask(for: objectID, progress: progress)
83+
for (mediaID, progress) in mediaInProgress {
84+
guard let media = media(withIdentifier: mediaID) else {
85+
continue
86+
}
87+
if media.remoteStatus == .pushing || media.remoteStatus == .processing {
88+
await scheduler.scheduleTask(for: TaggedManagedObjectID(media), progress: progress)
89+
}
7990
}
8091
}
8192

0 commit comments

Comments
 (0)