Swiftui에서 data가 변할때 view를 변화시켜줘야하는데 그때 사용하는것이 Combine이다. Combine은 RxSwift와 같은 기능을하고 API에서 받아오는 데이터도 mapping이 가능하다. 간단히 구성은 Publisher와 Subscriber로 나뉘어져있다.
let publisher = URLSession.shared.dataTaskPublisher(for: url)
let cancellable = publisher.sink(
receiveCompletion: { completion in
print(completion)
},
receiveValue: { value in
print(value)
}
)
let dataPublisher = publisher.map(\.data)
.decode(
type: Repository.self,
decoder: JSONDecoder()
)
.receive(on: DispatchQueue.main)
기본적인 Combine 사용법을 알아봤다. 다음 글에서 Combine의 HTTP Request 방법 등을 알아보도록 하겠다.