I am building an iOS app with RxSwift and RxDataSource using VIPER architecture. I want to change the content of the UICollectionView as the value of the presenter changes (as the user typed in some letters in the searchBar, the collectionView should show all users' profile that starts with the letters), but it doesn't work as I wanted it to.
By trying debug()
function, the value of presenter.section
in ViewController (which holds the content of the CollectionView) is changed after I typed in some letters in the searchBar. However, the collectionView is not reflecting the change.
Here are the main parts of the code.
code of ViewController
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.presenter.section
.drive(self.searchedFriendsCollectionView.rx.items(dataSource: self.dataSource))
.disposed(by: self.disposeBag)
self.searchedFriendsCollectoinView.rx
.setDelegate(self)
.disposed(by: self.disposeBag)
}
code of Presenter
init(view: SearchFriendsViewInterface, interactor:
SearchFriendsInteractorInterface, wireframe:
SearchFriendsWireframeInterface) {
self.view = view
self.interactor = interactor
self.wireframe = wireframe
let allUsers = self.interactor.allUsers().asObservable()
self.view.searchText
.debounce(0.5)
.filterNil()
.distinctUntilChanged()
.filterEmpty()
.asObservable()
.flatMap { text -> Observable<[SearchFriendsSection]> in
// get all users starting with "text"
allUsers.mapSections(query: text)
}
.bind(to: self.section)
.disposed(by: self.disposeBag)
}
extension Observable where E == [User] {
fileprivate func mapSections(query: String) -> Observable<[SearchFriendsSection]> {
return self.map {
$0.filter { $0.userDisplayName.hasPrefix(query) || $0.username.hasPrefix(query) }
}
.map { users -> [SearchFriendsSection] in
let items = users.map { user in
SearchFriendsItem(icon: user.profileImageURL, displayName: user.userDisplayName)
}
return [SearchFriendsSection(items: items)]
}
}
}
How I defined dataSource
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
self.searchedFriendsCollectoinView = UICollectionView(frame: .init(), collectionViewLayout: self.searchedFriendsCollectionViewFlowLayout)
let dataSource = RxCollectionViewSectionedReloadDataSource<SearchFriendsSection> (configureCell: {(_, collectionView, indexPath, item) -> UICollectionViewCell in
collectionView.register(SearchFriendsCell.self, forCellWithReuseIdentifier: "SearchFriendsCell")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchFriendsCell", for: indexPath) as! SearchFriendsCell
cell.item = item
return cell
})
self.dataSource = dataSource
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
- ViewController has the instance of Presenter, and the presenter has an instance called
section
. Thissection
holds the list of the users whose username is starting with specific letters.
Could you please help me? If I have left anything unclear, please let me know in the comment.
Update: output of the debug()
2019-02-01 10:22:27.610: values -> subscribed
2019-02-01 10:22:27.611: values -> Event next([])
--after typed in some letters in the searchBar--
2019-02-01 10:22:41.494: values -> Event
next([AppScreen.SearchFriendsSection(items:
[AppScreen.SearchFriendsItem(....),
AppScreen.SearchFriendsItem(....),
AppScreen.SearchFriendsItem(....)])])