๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IOS๐ŸŽ/์•„ํ‚คํ…์ณ

[์•„ํ‚คํ…์ณ] Viper ๋ž€

by Jouureee 2021. 11. 29.

์š”์ฆ˜ mvvm ํŒจํ„ด์ด ๋ฌด์—‡์ธ์ง€ ์•Œ๊ณ  ๋‚˜๋‹ˆ๊นŒ๋Š” ๋˜ ๋‹ค๋ฅธ ์•„ํ‚คํ…์ณ๋Š” ์—†๋‚˜ ๋งˆ๊ตฌ ๊นƒํ—™์„ ์ฐพ์•„๋ณด๋Š” ์ค‘์ด๋‹ค

์ž ๊น ์—ฌ์œ ๊ฐ€ ์ƒ๊ฒจ์„œ (๊ณง ๊ธฐ๋ง๊ณ ์‚ฌ + ๊ณผ์ œ + ํ…์Šค ์…ค .. ํฌํ ) viper ์•„ํ‚คํ…์ณ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๋ ค๊ตฌ ํ•œ๋‹ค !!

 

viper ์•„ํ‚คํ…์ณ๋Š” ์ดˆ๋ฉด์€ ์•„๋‹ˆ๋‹ค ๊ทธ๋ ‡๋‹ค๊ณ  ๊ตฌ๋ฉด๋„ ์•„๋‹ˆ์ง€๋งŒ์€ ํ•˜ํ•˜

์š”๊ฑธ ๊นƒํ—™ ์Šคํƒ€์—  ์ฐœํ•ด๋‘”๊ฒŒ ์ƒ๊ฐ๋‚˜์„œ ํŒจํ„ด์„ ์กฐ๊ธˆ ์‚ดํŽด๋ณด๊ณ  ์˜ˆ์ œ๋กœ๋„ ์‹ค์Šตํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

https://github.com/MindorksOpenSource/iOS-Viper-Architecture

 

GitHub - MindorksOpenSource/iOS-Viper-Architecture: This repository contains a detailed sample app that implements VIPER archite

This repository contains a detailed sample app that implements VIPER architecture in iOS using libraries and frameworks like Alamofire, AlamofireImage, PKHUD, CoreData etc. - GitHub - MindorksOpenS...

github.com

์šฐ์„  MVx ํŒจํ„ด๋“ค๊ณผ ๋‹ฌ๋ฆฌ ์ƒ‰๋‹ค๋ฅธ ์นœ๊ตฌ๋“ค์ด ๋งŽ์ด ๋ณด์ด๋Š”๊ฑฐ ๊ฐ™๋‹ค.

์šฐ์„  ์šฐ๋ฆฌ๊ฐ€ ์•„๋Š” view๋ถ€ํ„ฐ ์‚ดํŽด๋ณด์ž

 

View

ViewController๋ฅผ ์˜๋ฏธ, UI ๊ด€๋ จ ๋ถ€๋ถ„๋งŒ ๋‹ด๋‹นํ•œ๋‹ค.

Presenter๋ฅผ ์†Œ์œ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ(์˜์กด์ ) ์ด์— ๋”ฐ๋ผ ๋‹ด๋‹นํ•˜๋Š” UI๋ฅผ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.

 

 

Presenter

(์ œ์ผ ๊ฐ€์šด๋ฐ ์žˆ๋Š” ์นœ๊ตฌ๋กœ ์ œ์ผ ๋ฐ”๋น  ๋ณด์ธ๋‹ค)

View, Router. Interactor ์— ์˜์กด์ ์ด๋ฉฐ

View์—์„œ ๋ฐ›์€ ์‚ฌ์šฉ์ž ์ด๋ฒคํŠธ๋ฅผ ํ†ตํ•ด interactor์—๊ฒŒ ์—…๋ฐ์ดํŠธ ํ•  ๊ฒƒ์ธ์ง€ ๋ฌผ์–ด๋ณธ๋‹ค 

๋˜๋Š” Router๋ฅผ ํ†ตํ•ด ํ™”๋ฉด ์ด๋™์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

Interactor

๋ชจ๋ธ ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๋ฉฐ Network ํ†ต์‹ ์ด๋‚˜ entity์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  Presenter์—๊ฒŒ ์•Œ๋ฆฐ๋‹ค.

 

Router(WireFrame)

ํ™”๋ฉด ์ „ํ™˜๊ณผ ์˜์กด์„ฑ ์ฃผ์ž…์„ ๋‹ด๋‹นํ•œ๋‹ค.

 

Entity

๊ฐ€๊ณต๋˜๊ธฐ ์ „์˜ data model์ด๋‹ค.

 

ํŒจํ„ด์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์ด๊ฒŒ ๋‹ค๊ตฌ ..! ์‹ค์Šต์„ ํ†ตํ•ด ๋” ์‚ดํŽด๋ณด์ž

ํด๋”์˜ ๊ตฌ์„ฑ์€ ์œ„ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋˜์–ด ์žˆ๋Š”๋“ฏํ•˜๋‹ค

๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ Interactor, Presenter, View, WireFrame(Router)๋ฅผ ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

(Data Manager๋Š” Core Data๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ๊ณผ ๊ด€๋ จ์ด ์žˆ์œผ๋ฏ€๋กœ ์ƒ๋žตํ•œ๋‹ค)

 

์šฐ์„  ์•ฑ์ด ์ฒ˜์Œ ์‹คํ–‰๋ ๋•Œ ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€ appdelegate ํŒŒ์ผ๋ถ€ํ„ฐ ์‚ดํŽด๋ณด๊ฒ ๋‹ค.

Frame class๋Š” ์•ž์—์„œ ๋ณด์•˜๋˜๊ฒƒ๊ณผ ๊ฐ™์ด View ์ „ํ™˜์„ ๋‹ด๋‹นํ•œ๋‹ค. ๊ทธ๋ž˜์„œ createPostListModule ํ•จ์ˆ˜๋ฅผ ๋ถˆ๋Ÿฌ ์ฃผ์—ˆ๊ณ  PostList์—์„œ ์‚ฌ์šฉํ•  view, wireFrame, interactor, presenter, ๋“ฑ๋“ฑ ์„ ์ฃผ์ž…ํ•œ ๋’ค view ์ „ํ™˜์„  ํ•ด์ฃผ์—ˆ๋‹ค.

 

load๋œ postListView๋Š” 

Presenter์™€  Model์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค 

 

์‚ฌ์šฉ์ž ์ด๋ฒคํŠธ๋Š” Protocol๋กœ ์ผ์–ด๋‚  ์ด๋ฒคํŠธ๋“ค์„ ์ •๋ฆฌํ•ด๋‘์—ˆ๊ณ  

TableViewCell์„ ํด๋ฆญํ–ˆ์„ ๋•Œ 

presenter์—๊ฒŒ ํ–‰๋™์„ ์œ„์ž„ํ•œ๋‹ค. 

showPostDetail์„ ์ž์„ธํžˆ ๋ณด๋ฉด์€

wireFrame์ด ๋‚˜์„ค์ฐจ๋ก€๋ผ๋ฉฐ 

๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

์•„ ๊ทธ๋ฆฌ๊ณ  viewDidLoad์—์„œ ๊นœ๋ฐ•ํ•˜๊ณ  ๋†“์นœ๊ฒŒ ์žˆ๋Š”๋ฐ 

presenter.viewDidLoad ์š”๋ถ€๋ถ„ !์„ ๋”ฐ๋ผ๊ฐ€๋ฉด

interactor์—๊ฒŒ retrievePostListํ•˜๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ๋‚˜์˜จ๋‹ค. ๋˜ ๋”ฐ๋ผ๊ฐ€๋ณด๋ฉด 

PostListInteractor ํด๋ž˜์Šค์—์„œ๋Š” PostModel์˜ isEmpty, id ์—ฌ๋ถ€, error ๋‚ ๋•Œ ๋“ฑ ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ฅธ ๋‹ค์–‘ํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋”ฐ๋ผ ์–ด๋–ป๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค˜์•ผ ํ• ์ง€ ์ •์˜ํ•ด ๋†“๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋งˆ์น˜ MVVM ํŒจํ„ด์˜ View Model๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค. 

 

์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•œ tableview cell ํด๋ฆญ์‹œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ์˜ Viper ํŒจํ„ด์„ ์—ฟ๋ณด์•˜๋‹ค. ์ด๋ ‡๊ฒŒ apple์˜ MVC ํŒจํ„ด์—์„œ ๋ฒ—์–ด๋‚œ ๋…๋ฆฝ์ ์ธ class๋กœ ๊ตฌ์„ฑ๋œ ํŒจํ„ด์„ ๋ณด๋‹ˆ ios ๊ฐœ๋ฐœ์ด ๋‚ฏ์„ค๊ฒŒ ๋Š๊ปด์ง€๊ธฐ๋„ ํ•œ๋‹ค. ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋ฌธ๋ฒ•์„ ์ ์šฉํ•œ๋“ฏํ•œ .. ํ•˜์ง€๋งŒ ํŒจํ„ด์„ ํ๋ฆ„์— ๋”ฐ๋ผ ์ญ‰ ์ฝ์–ด์™€๋ดค๋“ฏ์ด 

 

Viper์˜ ์žฅ์ 

- ์žฌ์‚ฌ์šฉ์„ฑ๊ณผ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ๋ถ„๋ฆฌ

- ์ฑ…์ž„ ๋ถ„๋ฆฌ์˜ ์›์น™(SRP)์— ๋”ฐ๋ผ ์—ญํ• ์— ๋”ฐ๋ฅธ ๋ถ€๋ถ„ ๋ถ„๋ฆฌ

- ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ๋” ์šฉ์ด 

 

์ด ๋ฌด์—‡์ธ์ง€ ๊ณ ๊ฐœ ๋„๋•์ผ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๊ฑฐ ๊ฐ™๋‹ค. ํด๋ฆฐ์ฝ”๋“œ์Šค๋Ÿฝ .. protocol์„ ํ™œ์šฉํ•œ ๊ธฐ๋Šฅ ๋‚˜์—ด ๋ฐฉ์‹์„ ์ž์ฃผ ์ด์šฉํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค.

 

๋˜ ๊ธ€์„ ํฌ์ŠคํŒ…ํ•˜๊ธฐ ์œ„ํ•ด VIPER์˜ ์ฐธ๊ณ  ๋ฌธํ—Œ์„ ์‚ดํŽด๋ณด์•˜์„๋•Œ 

๋‚˜๋Š” ์—ฌํƒœ๊นŒ์ง€ MVVM์ด ์ตœ๊ณ ์ธ์ค„ ์•Œ์•˜๋Š”๋”” ๋ง์ด๋‹ค MVVM์€ rxswift

MVVM ์ฐธ๊ณ ํ•œ ๊ธ€์—์„œ MVVM์€ binding ๋•Œ๋ฌธ์— RxSwift๋ฅผ ์จ์•ผ ์ œ๋Œ€๋กœ ๊ตฌํ˜„์ด ๋œ๋‹ค๋Š” ๊ธ€์„ ๋ณด์•˜๋Š”๋ฐ Binding? ๊ทธ๋ฆฌ๊ณ  ์˜์กด์„ฑ ์ฃผ์ž…์— ๋Œ€ํ•ด ๋” ์‚ดํŽด ๋ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

 

 RiBs ํŒจํ„ด๋„ ๋” ์‚ดํŽด๋ณด๊ณ  MVVM ํŒจํ„ด์— ๋Œ€ํ•ด ๊ธ€๋„ ์จ๋ณด๊ณ  ๋” ๋‹ค์–‘ํ•œ ํŒจํ„ด์„ ๊ณต๋ถ€ํ•ด๋ณด๊ณ  ์‹ถ๋‹ค !!

 

 

 

๋Œ“๊ธ€