Skip to content

Commit 746792c

Browse files
Update NKCommon.swift
1 parent 2f9c2c7 commit 746792c

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

Sources/NextcloudKit/NKCommon.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ public struct NKCommon: Sendable {
101101
return completion(filesChunk, nil)
102102
}
103103

104-
defer {
105-
NotificationCenter.default.removeObserver(self, name: notificationCenterChunkedFileStop, object: nil)
106-
}
107-
108104
let fileManager = FileManager.default
109105
var isDirectory: ObjCBool = false
110106
var reader: FileHandle?
@@ -117,10 +113,6 @@ public struct NKCommon: Sendable {
117113
let bufferSize = 1_000_000
118114
var stop = false
119115

120-
NotificationCenter.default.addObserver(forName: notificationCenterChunkedFileStop, object: nil, queue: nil) { _ in
121-
stop = true
122-
}
123-
124116
// If max chunk count is > 10000 (max count), add + 100 MB to the chunk size to reduce the count. This is an edge case.
125117
let inputFilePath = inputDirectory + "/" + fileName
126118
let totalSize = getFileSize(filePath: inputFilePath)
@@ -148,6 +140,14 @@ public struct NKCommon: Sendable {
148140
return completion([], NSError(domain: "chunkedFile", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to open the input file for reading."]))
149141
}
150142

143+
let tokenObserver = NotificationCenter.default.addObserver(forName: notificationCenterChunkedFileStop, object: nil, queue: nil) { _ in
144+
stop = true
145+
}
146+
147+
defer {
148+
NotificationCenter.default.removeObserver(tokenObserver)
149+
}
150+
151151
outerLoop: repeat {
152152
if stop {
153153
return completion([], NSError(domain: "chunkedFile", code: -5, userInfo: [NSLocalizedDescriptionKey: "Chunking was stopped by user request or system notification."]))

0 commit comments

Comments
 (0)