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

[iOS] 1주차 피드백 반영 #61

Open
3 tasks done
dahun-lee-daji opened this issue Jun 17, 2021 · 4 comments
Open
3 tasks done

[iOS] 1주차 피드백 반영 #61

dahun-lee-daji opened this issue Jun 17, 2021 · 4 comments
Assignees
Labels
iOS Swift

Comments

@dahun-lee-daji
Copy link
Collaborator

dahun-lee-daji commented Jun 17, 2021

  • pickerView Coordinator패턴 적용
  • 다형성 및 의존성 주입 구현
  • forced unwrapping 제거 요망
@ellyheetov
Copy link
Collaborator

문제점

coordinator패턴을 도입하여 화면 전환의 책임을 넘겨 주었다. 그런데, ImagePicker객체는 ImagePickerController를 생성 및 present, dismiss하고 있으므로 coordinator 패턴을 잘 적용 하지 못하고 있다는 판단을 하게 되었다.

시도

coordinator에게 ImagePickerController 화면전환을 넘겨주기 위해서 다음과 같은 방법을 시도해보았다.

1. dissmiss할 controller를 coordinator에게 매개변수로 넘겨주기

func dismiss(view: UIViewController) {
        view.dismiss(animated: true, completion: nil)
}

그런데, UIViewController를 매개변수로 넘겨주는 것은 기피하란다.

왜? 🤔

UIViewController를 넘겨주면 해당 매소드내에서 무슨일이 일어날지 알 수 없으므로! Controller를 외부에서 접근하고 변경하는 것은 의존성의 문제도 야기할 수 있다고 생각한다.

2. ImagePickerController를 Coordinator에서 생성하기

Coordinator에서 ImagePickerController를 생성하고, present & dismiss 하려는 시도를 했다.

그러나, UIImagePickerControllerDelegate 메소드를 살펴 볼 필요가 있다.
UIImagePickerControllerDelegate의 메소드는 두가지의 메소드 구현을 요한다.

 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])

두가지 메소드는 UIPickerController를 매개변수로 받고 있다. 해당 매개변수를 이용하여 UIImagePickerController를 present, dismiss 할 수 있는 것이다. 만약 이러한 present, dismiss를 coordinator에게 넘겨주고 싶다면, Coordinator가 UIImagePickerControllerDelegate를 채택하게 되는 이상한 구조가 탄생한다. (아니면, 내부에서 1번 방법을 택하던지)

3. Coordinator가 ImagePickerController를 기억하기

Coordinator에서 ViewController를 기억할 필요가 있을까?
Coordinator는 모든 ViewController를 알고 있어야 하는걸까?

필요할 때 ImagePickerController를 생성하고, 다 사용 이후에는 메모리에서 제거하는 방법도 있겠지만, 여전히 Coordinator는 ImagePickerController를 프로퍼티로 가지고 있어야 한다.

좋은 방법인지 모르겠으므로 일단 보류.

4. Coordinator에서 마지막 ViewController dismiss하기

func dismiss() {
    self.navigationController.popViewController(animated: true)
}

Coordinator가 가지고 있는 NavigationController에 ImagePickerController를 push했으므로, pop을 해주면 된다.
그런데, navigationController 스택에 가장 마지막에 쌓인 컨트롤러가 ImagePickerController라는 것을 보장 할 수 있는 지는 모르겠다.

결론

화면 전환을 위해서 UIImagePickerControllerDelegate 프로토콜 내부 메소드가 ViewController를 매개변수로 넘겨주는 만큼
coordinator에서 매개변수로 ViewController를 받아 화면 전환 하는 것이 낫다고 판단!

@ellyheetov
Copy link
Collaborator

@dahun-lee-daji
그래서 제가 하고 싶은 말은 지금 구조를 변경하지 않는게 좋겠다는 말입니당😃
그냥 "다우니가 한게 최선이예요", "바꾸지 않는 게 좋겠어요" 라고 말씀 드리는 것보다, 정확한 근거를 들어서 말씀드리는 게 좋을것 같아서 공유해드려요. (제가 말보다는 글이 편해서😉)

@dahun-lee-daji
Copy link
Collaborator Author

dahun-lee-daji commented Jun 21, 2021

방법은 정말 여러가지인데, 전부다 맘에드는 방법을 찾기가 어렵네요!

혹시 이런 방법은 어떨까요? 갑자기 생각났어요

새로운 delegate instance를 정의합니다.
이 delegate의 func은 delegate를 소유하고 있는 VC의 view를 dismiss합니다.
각 VC는 의존성 주입을 통해 delegate를 갖고, 해제되어야 할 때 delegate의 func을 사용하여 dismiss하는 방법입니다!

아 좀 바보같은 의견이네요 coordinator가 dismiss의 책임을 갖고있어야는데 ;;;;;

@ellyheetov
Copy link
Collaborator

ㅋㅋㅋ결국 그 Delegate instance가 coordinator인거죠

dahun-lee-daji added a commit that referenced this issue Jun 22, 2021
그에 따른 VC instance 생성 구문 변경
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iOS Swift
Projects
None yet
Development

No branches or pull requests

2 participants