[https://www.youtube.com/watch?v=HnWWo0b4v9w&list=PLqj8V2bxPxpDRRpKl01DS5qc3ImXb_MYg&index=10](NetflixClone: SearchViewController 1)
private let searchController: UISearchController = {
let results = SearchResultViewController()
let vc = UISearchController(searchResultsController: results)
vc.searchBar.placeholder = "검색"
vc.searchBar.searchBarStyle = .minimal
vc.definesPresentationContext = true
return vc
}()
present
하고 있는 뷰 컨트롤러 단에서 사용 가능let publisher = NotificationCenter.default.publisher(for: UISearchTextField.textDidChangeNotification, object: searchController.searchBar.searchTextField)
publisher
.compactMap({$0.object as? UISearchTextField})
.map({ $0.text })
.debounce(for: .milliseconds(500), scheduler: DispatchQueue.main)
.sink { [weak self] queryString in
guard
let queryString = queryString,
!queryString.trimmingCharacters(in: .whitespaces).isEmpty,
let resultVC = self?.searchController.searchResultsController as? SearchResultViewController else { return }
resultVC.getSearchedResult(with: queryString)
}
.store(in: &cancellables)
resultVC
의 getSearchedResult
함수를 실행하는 바인딩 구문func getSearchedResult(with query: String) {
viewModel
.getSearchResultContents(with: query)
}
func getSearchResultContents(with query: String) {
APICaller
.shared
.getSearchResultContents(with: query)
.sink { completion in
switch completion {
case .failure(let error): print(error.localizedDescription)
case .finished: break
}
} receiveValue: { [weak self] resultModels in
print(resultModels)
self?.searchResultsModel.send(resultModels)
}
.store(in: &cancellables)
}