Skip to content

Commit 4492c9f

Browse files
committed
refactor(UploadService): Dedicated queues for events and rebuilding the upload queues
1 parent 1c888de commit 4492c9f

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

kDriveCore/Data/Upload/Servicies/UploadService/UploadService+Notifications.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extension UploadService: UploadNotifiable {
4848

4949
public func sendFileUploadStateNotificationIfNeeded(with result: UploadCompletionResult) {
5050
Log.uploadQueue("sendFileUploadStateNotificationIfNeeded")
51-
serialQueue.async { [weak self] in
51+
serialEventQueue.async { [weak self] in
5252
guard let self else { return }
5353
guard let uploadFile = result.uploadFile,
5454
uploadFile.error != .taskRescheduled,

kDriveCore/Data/Upload/Servicies/UploadService/UploadService+Observation.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension UploadService: UploadObservable {
4949
fileId: String? = nil,
5050
using closure: @escaping (UploadFile, File?) -> Void) -> ObservationToken {
5151
var token: ObservationToken!
52-
serialQueue.sync { [weak self] in
52+
serialEventQueue.sync { [weak self] in
5353
guard let self else { return }
5454
let key = UUID()
5555
observations.didUploadFile[key] = { [weak self, weak observer] uploadFile, driveFile in
@@ -81,7 +81,7 @@ extension UploadService: UploadObservable {
8181
parentId: Int,
8282
using closure: @escaping (Int, Int) -> Void) -> ObservationToken {
8383
var token: ObservationToken!
84-
serialQueue.sync { [weak self] in
84+
serialEventQueue.sync { [weak self] in
8585
guard let self else { return }
8686
let key = UUID()
8787
observations.didChangeUploadCountInParent[key] = { [weak self, weak observer] updatedParentId, count in
@@ -111,7 +111,7 @@ extension UploadService: UploadObservable {
111111
driveId: Int,
112112
using closure: @escaping (Int, Int) -> Void) -> ObservationToken {
113113
var token: ObservationToken!
114-
serialQueue.sync { [weak self] in
114+
serialEventQueue.sync { [weak self] in
115115
guard let self else { return }
116116
let key = UUID()
117117
observations.didChangeUploadCountInDrive[key] = { [weak self, weak observer] updatedDriveId, count in

kDriveCore/Data/Upload/Servicies/UploadService/UploadService+Publish.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension UploadService: UploadPublishable {
4343
userId: Int,
4444
driveId: Int) {
4545
Log.uploadQueue("publishUploadCount")
46-
serialQueue.async { [weak self] in
46+
serialEventQueue.async { [weak self] in
4747
guard let self else { return }
4848
publishUploadCountInParent(parentId: parentId, userId: userId, driveId: driveId)
4949
publishUploadCountInDrive(userId: userId, driveId: driveId)
@@ -54,7 +54,7 @@ extension UploadService: UploadPublishable {
5454
userId: Int,
5555
driveId: Int) {
5656
Log.uploadQueue("publishUploadCountInParent")
57-
serialQueue.async { [weak self] in
57+
serialEventQueue.async { [weak self] in
5858
guard let self else { return }
5959

6060
let uploadCount = getUploadingFiles(withParent: parentId, userId: userId, driveId: driveId).count
@@ -69,7 +69,7 @@ extension UploadService: UploadPublishable {
6969
public func publishUploadCountInDrive(userId: Int,
7070
driveId: Int) {
7171
Log.uploadQueue("publishUploadCountInDrive")
72-
serialQueue.async { [weak self] in
72+
serialEventQueue.async { [weak self] in
7373
guard let self else { return }
7474
let uploadCount = getUploadingFiles(userId: userId, driveId: driveId).count
7575
for closure in observations.didChangeUploadCountInDrive.values {
@@ -84,7 +84,7 @@ extension UploadService: UploadPublishable {
8484
Log.uploadQueue("publishFileUploaded")
8585
logFileUploadedWithSuccess(for: result.uploadFile)
8686
sendFileUploadStateNotificationIfNeeded(with: result)
87-
serialQueue.async { [weak self] in
87+
serialEventQueue.async { [weak self] in
8888
guard let self else { return }
8989
for closure in observations.didUploadFile.values {
9090
guard let uploadFile = result.uploadFile, !uploadFile.isInvalidated else {

kDriveCore/Data/Upload/Servicies/UploadService/UploadService.swift

+15-4
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,24 @@ public final class UploadService {
3232
@LazyInjectService var notificationHelper: NotificationsHelpable
3333
@LazyInjectService var appContextService: AppContextServiceable
3434

35-
let serialQueue: DispatchQueue = {
35+
private let serialRebuildUploadsQueue: DispatchQueue = {
3636
@LazyInjectService var appContextService: AppContextServiceable
3737
let autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency = appContextService.isExtension ? .workItem : .inherit
3838

3939
return DispatchQueue(
40-
label: "com.infomaniak.drive.upload-service",
41-
qos: .userInitiated,
40+
label: "com.infomaniak.drive.upload-service.rebuild-uploads",
41+
qos: .default,
42+
autoreleaseFrequency: autoreleaseFrequency
43+
)
44+
}()
45+
46+
let serialEventQueue: DispatchQueue = {
47+
@LazyInjectService var appContextService: AppContextServiceable
48+
let autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency = appContextService.isExtension ? .workItem : .inherit
49+
50+
return DispatchQueue(
51+
label: "com.infomaniak.drive.upload-service.event",
52+
qos: .default,
4253
autoreleaseFrequency: autoreleaseFrequency
4354
)
4455
}()
@@ -87,7 +98,7 @@ extension UploadService: UploadServiceable {
8798

8899
public func rebuildUploadQueueFromObjectsInRealm() {
89100
Log.uploadQueue("rebuildUploadQueueFromObjectsInRealm")
90-
serialQueue.sync {
101+
serialRebuildUploadsQueue.sync {
91102
// Clean cache if necessary before we try to restart the uploads.
92103
@InjectService var freeSpaceService: FreeSpaceService
93104
freeSpaceService.cleanCacheIfAlmostFull()

0 commit comments

Comments
 (0)