-
Notifications
You must be signed in to change notification settings - Fork 0
컴바인 스터디 과제 #2
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
base: main
Are you sure you want to change the base?
컴바인 스터디 과제 #2
Conversation
Yeonnies
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| import SwiftUI | ||
|
|
||
| struct DailyBoxOfficeView: View { | ||
| @StateObject var viewModel: DailyBoxOfficeViewModel_Combine |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 혹시 @ObjervedObject가 아닌 @StateObject 쓰신 이유가 있나요??
|
|
||
| import SwiftUI | ||
|
|
||
| protocol ViewModelType: ObservableObject { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
왜 아웃풋은 따로 안만들어줬나요?? 바보 질문 미안..
| import Foundation | ||
|
|
||
| class DailyBoxOfficeViewModel_Combine: ViewModelType { | ||
| private let networkService = CombineBoxOfficeService() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
싱글톤 패턴으로 서비스 만드셨던 거 같은데 굳이 만들어주신 이유가 있나요?? 바보 질문 죄송
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이것은저의바보짓입니다

🌀 컴바인 과제
📂 시연 영상
구현
처음에 그냥 Published + Task로 네트워크 호출을 했더니 컴바인을 안쓰게 되는 코드가 되더라구요
그래서 컴바인을 사용하는 버전, 안사용하는 버전 두가지의 뷰모델을 만들었습니다.
PR에서는 컴바인을 사용하는 버전만 작성합니다. + 저는 스유로 과제했습니다
네트워크
AnyPublisher를 리턴해줍니다. 퍼블리셔에서는 model 배열을 담고있습니다
뷰모델
저는 뷰모델타입 프로토콜을 채택해서 ViewModelType 프로토콜에서 ObservableObject를 채택해서 사용했습니다
퍼블리셔를 구독해서 Published 프로퍼티 객체에 해당 값을 바인딩해줍니다. 그러면 뷰에서 ui가 갱신되게 됩니다.
참고자료
https://medium.com/@catchvarun25/mvvm-with-swiftui-combine-51edb1a011ce