Skip to content

Commit e5e3e7b

Browse files
committed
[Refactor] #182 - 잘못된 OOM 크래시 방어코드 수정
1 parent e654d03 commit e5e3e7b

1 file changed

Lines changed: 12 additions & 24 deletions

File tree

Neki-iOS/Core/Sources/ImagePicker/Domain/ImageUploadClient.swift

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,21 @@ extension ImageUploadClient: DependencyKey {
2525
},
2626

2727
uploadConcurrentlyFromURLs: { fileURLs, mediaType in
28-
return try await withThrowingTaskGroup(of: [Int].self) { group in
29-
var uploadedMediaIDs: [Int] = []
30-
let maxConcurrentTasks = 3
31-
var urlIterator = fileURLs.makeIterator()
32-
33-
for _ in 0..<maxConcurrentTasks {
34-
guard let url = urlIterator.next() else { break }
35-
group.addTask {
36-
let data = try Data(contentsOf: url)
37-
let entity = ImageUploadEntity(data: data, format: data.detectedImageFormat)
38-
return try await repository.upload(items: [entity], mediaType: mediaType)
39-
}
40-
}
41-
42-
for try await resultIDs in group {
43-
uploadedMediaIDs.append(contentsOf: resultIDs)
44-
45-
guard let nextURL = urlIterator.next() else { continue }
46-
group.addTask {
47-
let data = try Data(contentsOf: nextURL)
48-
let entity = ImageUploadEntity(data: data, format: data.detectedImageFormat)
49-
return try await repository.upload(items: [entity], mediaType: mediaType)
50-
}
28+
var uploadedMediaIDs: [Int] = []
29+
let chunkSize: Int = 3
30+
let chunks = stride(from: 0, to: fileURLs.count, by: chunkSize).map { Array(fileURLs[$0..<min($0 + chunkSize, fileURLs.count)]) }
31+
32+
for chunk in chunks {
33+
var entities: [ImageUploadEntity] = []
34+
for url in chunk {
35+
let data = try Data(contentsOf: url)
36+
entities.append(ImageUploadEntity(data: data, format: data.detectedImageFormat))
5137
}
5238

53-
return uploadedMediaIDs
39+
let resultIDs = try await repository.upload(items: entities, mediaType: mediaType)
40+
uploadedMediaIDs.append(contentsOf: resultIDs)
5441
}
42+
return uploadedMediaIDs
5543
},
5644

5745
convert: { items in

0 commit comments

Comments
 (0)