Skip to content

Commit 1ce20c0

Browse files
committed
Remove async and assert running on the main thread
1 parent 1de9146 commit 1ce20c0

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

WordPress/Classes/Services/MediaCoordinator.swift

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

456-
@MainActor
457-
func submitBackgroundUploadTask() async {
458-
await mediaLibraryProgressCoordinator.submitBackgroundUploadTask()
456+
func submitBackgroundUploadTask() {
457+
mediaLibraryProgressCoordinator.submitBackgroundUploadTask()
459458
}
460459

461460
// MARK: - Progress

WordPress/Classes/System/WordPressAppDelegate.swift

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

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

201199
public func applicationDidBecomeActive(_ application: UIApplication) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,15 @@ public class MediaProgressCoordinator: NSObject {
7676

7777
/// Utilize BGContinuedProcessingTask to show upload progress and extend the allowed background time for the upload.
7878
/// Note: This function needs to be called before the app goes to the background.
79-
@MainActor
80-
func submitBackgroundUploadTask() async {
79+
func submitBackgroundUploadTask() {
8180
guard let scheduler = mediaUploadBackgroundTaskScheduler() else { return }
8281

8382
for (mediaID, progress) in mediaInProgress {
8483
guard let media = media(withIdentifier: mediaID) else {
8584
continue
8685
}
8786
if media.remoteStatus == .pushing || media.remoteStatus == .processing {
88-
await scheduler.scheduleTask(for: TaggedManagedObjectID(media), progress: progress)
87+
scheduler.scheduleTask(for: TaggedManagedObjectID(media), progress: progress)
8988
}
9089
}
9190
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import WordPressShared
66
// This protocol is used to hide the `@available(iOS 26.0, *)` check.
77
protocol MediaUploadBackgroundTaskScheduler {
88

9-
func scheduleTask(for media: TaggedManagedObjectID<Media>, progress: Progress) async
9+
/// Create a `BGContinuedProcessingTask` to get extra background time for the media uploading.
10+
///
11+
/// Note: This method must be called from the main thread.
12+
func scheduleTask(for media: TaggedManagedObjectID<Media>, progress: Progress)
1013

1114
}
1215

@@ -19,8 +22,10 @@ func mediaUploadBackgroundTaskScheduler() -> MediaUploadBackgroundTaskScheduler?
1922
}
2023

2124
/// Utilize `BGContinuedProcessingTask` to show the uploading media activity.
25+
///
26+
/// Due to how media uploading is implemented currently, we need to read the uploading state from the main thread.
27+
/// For better code readability, this type is implemented in the same way where everything runs on the main thread.
2228
@available(iOS 26.0, *)
23-
@MainActor
2429
private class ConcreteMediaUploadBackgroundTaskScheduler: MediaUploadBackgroundTaskScheduler {
2530
struct Item {
2631
// Please note: all media query needs to be done in the main context, due to the current upload media implementation.
@@ -80,6 +85,8 @@ private class ConcreteMediaUploadBackgroundTaskScheduler: MediaUploadBackgroundT
8085
}
8186

8287
func scheduleTask(for media: TaggedManagedObjectID<Media>, progress: Progress) {
88+
wpAssert(Thread.isMainThread)
89+
8390
observeCoreDataChanges()
8491

8592
let item = Item(media: media, progress: progress)
@@ -148,6 +155,10 @@ private class ConcreteMediaUploadBackgroundTaskScheduler: MediaUploadBackgroundT
148155
}
149156

150157
self.state = .accepted(accepted)
158+
159+
// Immediately update the `BGContinuedProcessingTask` instance with the current media uploading status.
160+
self.handleProgressUpdates()
161+
self.handleStatusUpdates()
151162
}
152163

153164
private func observe(_ item: Item, accepted: inout BGTaskState.Accepted) {

0 commit comments

Comments
 (0)