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

feat: 업로드용 메서드 구현 #197

Merged
merged 5 commits into from
Dec 5, 2023
Merged

feat: 업로드용 메서드 구현 #197

merged 5 commits into from
Dec 5, 2023

Conversation

loinsir
Copy link
Collaborator

@loinsir loinsir commented Dec 4, 2023

🧑‍🚀 PR 요약

  • 이미지 업로드용 업로드 메서드 구현
  • 동영상 업로드용 백그라운드 업로드 메서드 구현

📌 변경 사항

Note

백그라운드 업로드 메서드 사용법

  • 앱에서 progressBar를 표현하려면 업로드가 얼마나 진행되었는지 observing해야 하고,
    이는 URLSessionTaskDelegate의 didSendBodyData 인자가 있는 메서드를 통해 구현할 수 있습니다.
  • 그래서 provider에서 해당 백그라운드 업로드 메서드를 호출하는 곳을 delegate로 주입할 수 있도록 인자를 열어두었습니다.
  • 또한 해당 이벤트를 어떤 OperationQueue에서 받을 건지 지정해주어야 하는데, 우리는 보통 UI에 표기하기 위해 main에서 받을 테니까
    OperationQueue.main을 넘기면 될 것 같네요
func backgroundUpload(fromFile: URL,
                          to url: String,
                          method: HTTPMethod = .PUT,
                          sessionTaskDelegate: URLSessionTaskDelegate? = nil,
                          delegateQueue: OperationQueue? = nil) async throws -> Data {
    guard let url = URL(string: url) else { throw NetworkError.components }
    var request = URLRequest(url: url)
    request.httpMethod = method.rawValue
    let backgroundSession = URLSession(configuration: .background(withIdentifier: UUID().uuidString),
                                       delegate: sessionTaskDelegate,
                                       delegateQueue: delegateQueue)
    let (data, response) = try await backgroundSession.upload(for: request, fromFile: fromFile)
    try self.checkStatusCode(of: response)
    return data
}

Linked Issue

close #187

@loinsir loinsir linked an issue Dec 4, 2023 that may be closed by this pull request
1 task
Copy link
Collaborator

@chopmozzi chopmozzi left a comment

Choose a reason for hiding this comment

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

업로드 메소드만 있는 걸로 확인하고 approve하겠습니다. 👍

// 이미지 업로드용
func upload(data: Data, to url: String, method: HTTPMethod = .PUT) async throws -> Data {
guard let url = URL(string: url) else { throw NetworkError.components }
var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData)
Copy link
Collaborator

Choose a reason for hiding this comment

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

저희 API중에 cache를 리턴하는 api가 있나요?

Copy link
Collaborator Author

@loinsir loinsir Dec 5, 2023

Choose a reason for hiding this comment

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

URL Loading System은 내부적으로 응답에 대해 캐싱을 하는데요,
업로드 메서드 같은 경우 매번 요청에 대한 응답을 받는 것을 보장시키기 위해 저런 cachePolicy 정책을 지정했습니다.

Comment on lines +115 to +129
func backgroundUpload(fromFile: URL,
to url: String,
method: HTTPMethod = .PUT,
sessionTaskDelegate: URLSessionTaskDelegate? = nil,
delegateQueue: OperationQueue? = nil) async throws -> Data {
guard let url = URL(string: url) else { throw NetworkError.components }
var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData)
request.httpMethod = method.rawValue
let backgroundSession = URLSession(configuration: .background(withIdentifier: UUID().uuidString),
delegate: sessionTaskDelegate,
delegateQueue: delegateQueue)
let (data, response) = try await backgroundSession.upload(for: request, fromFile: fromFile)
try self.checkStatusCode(of: response)
return data
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

아직 observing이나 다른 부분들은 남은거죠?

Copy link
Collaborator Author

@loinsir loinsir Dec 5, 2023

Choose a reason for hiding this comment

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

위에 적었듯이 URLSessionTaskDelegate로 observing 할 수 있습니다.

Copy link
Member

Choose a reason for hiding this comment

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

아 여기에 업로드 메소드 호출하는 객체를 주입해서 쓰면 되는거군요

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵. interactor나 worker에 delegate 채택해서 보내면 될 것 같습니다

Copy link
Member

@anyukyung anyukyung left a comment

Choose a reason for hiding this comment

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

수고하셨습니다 🎀

@loinsir loinsir merged commit 982060f into iOS/dev Dec 5, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: provider에 데이터 업로드 메서드 구현
3 participants