From 3f129ab6e017392e854194c39f552a36fcc355e2 Mon Sep 17 00:00:00 2001 From: LURKS02 Date: Fri, 29 Nov 2024 19:20:10 +0900 Subject: [PATCH] =?UTF-8?q?[REFACTOR]=20completion=20=EB=8C=80=EC=8B=A0=20?= =?UTF-8?q?continuation=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Data/Data/SharingVideoRepository.swift | 27 +++++++++++++------------- Data/P2PSocket/SocketProvidable.swift | 5 ++--- Data/P2PSocket/SocketProvider.swift | 7 ++----- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Data/Data/SharingVideoRepository.swift b/Data/Data/SharingVideoRepository.swift index ee5a4042..8504bc59 100644 --- a/Data/Data/SharingVideoRepository.swift +++ b/Data/Data/SharingVideoRepository.swift @@ -102,21 +102,20 @@ private extension SharingVideoRepository { } func sendFiles(fileNames: [String], to peerID: String, isRequested: Bool) { - guard !fileNames.isEmpty else { - sendResponse(isRequested: isRequested, to: peerID) - return - } - - for index in fileNames.indices { - let fileName = fileNames[index] - let fileURL = FileSystemManager.shared.folder.appending(component: fileName) - let completion: (((any Error)?) -> Void)? = (index == fileNames.count - 1) ? { _ in - self.sendResponse(isRequested: isRequested, to: peerID) } : nil + Task { + for index in fileNames.indices { + let fileName = fileNames[index] + let fileURL = FileSystemManager.shared.folder.appending(component: fileName) + + await socketProvider.sendResource(url: fileURL, + resourceName: fileName, + to: peerID) + sendSyncFlags[peerID] = true + } - socketProvider.sendResource(url: fileURL, - resourceName: fileName, - to: peerID, - completion: completion) + if isRequested { + sendReceivedResponse(to: peerID) + } } } diff --git a/Data/P2PSocket/SocketProvidable.swift b/Data/P2PSocket/SocketProvidable.swift index a06e0a7a..56c6e822 100644 --- a/Data/P2PSocket/SocketProvidable.swift +++ b/Data/P2PSocket/SocketProvidable.swift @@ -54,9 +54,8 @@ public protocol SocketResourceSendable { func sendResource( url localURL: URL, resourceName: String, - to peerID: String, - completion: (((any Error)?) -> Void)? - ) + to peerID: String + ) async /// 연결된 모든 Peer들에게 리소스를 전송합니다. func sendResourceToAll(url: URL, resourceName: String) async throws /// Peer들과 공유한 모든 리소스를 리턴합니다. diff --git a/Data/P2PSocket/SocketProvider.swift b/Data/P2PSocket/SocketProvider.swift index e47ccf84..8820bb7a 100644 --- a/Data/P2PSocket/SocketProvider.swift +++ b/Data/P2PSocket/SocketProvider.swift @@ -144,17 +144,14 @@ public extension SocketProvider { func sendResource( url localURL: URL, resourceName: String, - to peerID: String, - completion: (((any Error)?) -> Void)? = nil - ) { + to peerID: String) async { let uuid = UUID() let nameWithUUID = [resourceName, uuid.uuidString].joined(separator: "/") guard let mcPeerID = MCPeerIDStorage.shared.peerIDByIdentifier[peerID]?.id else { return } session.sendResource(at: localURL, withName: nameWithUUID, - toPeer: mcPeerID, - withCompletionHandler: completion) + toPeer: mcPeerID) } func sendResourceToAll(url localUrl: URL, resourceName: String) async throws {