업데이트를 해보자 ~~
<aside> ✏️ 2022.11.03
아 인풋아웃풋 빼먹었다.
</aside>
→ 코드 개선 (DiffableDataSource + CompositionalLayout / RxSwift + RxCocoa)
검색화면
MVVM 패턴을 적용
Model - View - ViewModel
final class SearchViewModel {
var isFiltering: BehaviorRelay<Bool> = BehaviorRelay(value: false)
var location = ["강남구", "강동구", "강북구", "강서구", "관악구", "광진구", "구로구", "금천구", "노원구", "도봉구", "동대문구", "동작구", "마포구", "서대문구", "서초구", "성동구", "성북구", "송파구", "양천구", "영등포구", "용산구", "은평구", "종로구", "중구", "중랑구"]
var filteredLocation: BehaviorRelay<[String]> = BehaviorRelay(value: [])
var numberOfRowsInSection: Int {
return filteredLocation.value.count
}
func cellForRowAt(at indexPath: IndexPath) -> String {
return filteredLocation.value[indexPath.row]
}
func filterLocation(_ text: String) {
let filtered = location.filter { $0.contains(text) }
filteredLocation.accept(filtered)
}
}
행정구 정보가 모델에 해당
뷰에서 검색창에 검색어를 입력 받아서 → 이 검색어를 바탕으로 필터링을 하는 것 ⇒ 이 인터렉션과 이에 대한 반응을 뷰모델에서 관리 (= 비즈니스 로직)
private func bind() {
viewModel.filteredLocation.accept(viewModel.location)
viewModel.filteredLocation
.withUnretained(self)
.bind { vc, location in
vc.rootView.tableView.reloadData()
}
.disposed(by: disposeBag)
searchBar.rx.text.orEmpty
.debounce(RxTimeInterval.microseconds(5), scheduler: MainScheduler.instance)
.withUnretained(self)
.bind { vc, value in
if value != "" {
vc.viewModel.isFiltering.accept(true)
vc.viewModel.filterLocation(value)
}
}
.disposed(by: disposeBag)
searchBar.rx.searchButtonClicked
.withUnretained(self)
.bind { vc, _ in
vc.searchBar.resignFirstResponder()
}
.disposed(by: disposeBag)
viewModel.filteredLocation
.bind(to: rootView.tableView.rx.items(cellIdentifier: MainSearchTableViewCell.reuseIdentifier, cellType: MainSearchTableViewCell.self)) { [weak self] index, item, cell in
guard let self = self else { return }
if let text = self.searchBar.text {
if self.viewModel.isFiltering.value {
cell.setData(item, true, text)
} else {
cell.setData(item, false, text)
}
}
}
.disposed(by: disposeBag)
rootView.tableView.rx
.itemSelected
.withUnretained(self)
.subscribe(onNext: { (vc, value) in
vc.locationClosure?(vc.viewModel.filteredLocation.value[value.row])
vc.navigationController?.popViewController(animated: true)
})
.disposed(by: disposeBag)
}
→ 뷰에서 인터랙션을 담당
차량 거점지 상세 화면
→ 여기까지 적용 후, 테스트 플라이트에서 빌드 확인
문제는 .. 왜 메모리가 갑자기 많아지는가?
→ 어디서 메모리가 그렇게 많아지는 것인지?