Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] NearbyNetwork 데이터 송수신 기능 리팩터링 #152

Merged
merged 2 commits into from
Jan 14, 2025

Conversation

choijungp
Copy link
Collaborator

🌁 Background

MPC 프레임워크를 Network 프레임 워크로 교체하며 데이터 송수신 기능을 리팩터링 하였습니다.

👩‍💻 Contents

  • 데이터 송신
  • 데이터 수신

📝 Review Note

데이터 송신 실패 시 반복문을 통해 3회까지 재전송하도록 하였습니다.

private func send(data: DataInformationDTO, connection: NWConnection) async -> Bool {
    typealias Continuation = CheckedContinuation<Bool, Never>
    var tryCount = 0

    let encodedData = try? jsonEncoder.encode(data)
    let message = NWProtocolFramer.Message(nearbyNetworkMessageType: .data)
    let context = NWConnection.ContentContext(identifier: "Data", metadata: [message])

    while tryCount < 3 {
        let result = await withCheckedContinuation { (continuation: Continuation) in
            connection.send(
                content: encodedData,
                contentContext: context,
                completion: .contentProcessed({ error in
                    if let error {
                        continuation.resume(returning: false)
                    } else {
                        continuation.resume(returning: true)
                    }
                }))
        }

        if result { return true }
        tryCount += 1
    }

    return false
}

📣 Related Issue

Copy link
Member

@eemdeeks eemdeeks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!!

완벽하네요 :)

Comment on lines +123 to +139
while tryCount < 3 {
let result = await withCheckedContinuation { (continuation: Continuation) in
connection.send(
content: encodedData,
contentContext: context,
completion: .contentProcessed({ error in
if let error {
continuation.resume(returning: false)
} else {
continuation.resume(returning: true)
}
}))
}

if result { return true }
tryCount += 1
}
Copy link
Member

@eemdeeks eemdeeks Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withCheckedContinuation으로 클로저 내부의 error에 따른 처리를 해 주셨군요..??
다른 방법도 있을 것 같은데, 이 코드를 보니.. 이코드밖에 생각이 안날 것 같네요!(그만큼 훌륭하단 뜻) 좋습니다!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

우선 Continuation을 사용한 이유는 send의 결과 값을 반환하기 위해 불가피하게 wrapping 하였습니다!
저희가 고려한 다른 방법은 완료 핸들러를 통해 send 결과를 전달하는 방식이었는데요! 이 방식보다 continuation으로 감싸 결과 값을 직접 반환하는 편이 가독성과 코드 유지 보수 측면에서 더 낫다고 판단했습니다!

Copy link
Collaborator

@ekrud99 ekrud99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

띵쪼 고생 많으셨습니다 😄

@@ -150,6 +150,10 @@ extension NearbyNetworkService: NearbyNetworkInterface {
}
}

public func send(data: DataInformationDTO) async -> Bool {
return true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

갑자기 이거 보고 생각났는데..
저는 딴이랑 페프할 때 한줄이면 return 생략했거덩요?
별거 아니니까 그냥 무시시 고고??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

허얼 ~ 1줄이여도 노 생략인게 저희 컨벤션으루 기억합니다만 !!??!!!!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇.. 저희 명시적으로 작성하려구 한 줄 일때도 return 생략하지 않기로 했던 것으로 기억합니다!

@taipaise taipaise merged commit 932d6c7 into develop Jan 14, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants