I'm facing a problem when selecting the table view row on RxSwift. For details, the code on the do(onNext:) function is called twice, thus lead to the navigation pushed twice too. Here is my code in the viewModel, please help me resolve it. Thanks so much.
struct Input {
let loadTrigger: Driver<String>
let searchTrigger: Driver<String>
let selectMealTrigger: Driver<IndexPath>
}
struct Output {
let mealList: Driver<[Meal]>
let selectedMeal: Driver<Meal>
}
func transform(_ input: HomeViewModel.Input) -> HomeViewModel.Output {
let popularMeals = input.loadTrigger
.flatMap { _ in
return self.useCase.getMealList()
.asDriver(onErrorJustReturn: [])
}
let mealSearchList = input.searchTrigger
.flatMap { text in
return self.useCase.getMealSearchList(mealName: text)
.asDriver(onErrorJustReturn: [])
}
let mealList = Observable.of(mealSearchList.asObservable(), popularMeals.asObservable()).merge().asDriver(onErrorJustReturn: [])
let selectedMeal = input.selectMealTrigger
.withLatestFrom(mealList) { $1[$0.row] }
.do(onNext: { meal in
self.navigator.toMealDetail(meal: meal)
})
return Output(mealList: mealList, selectedMeal: selectedMeal)
}
Edit: Here's the implemetation on the ViewController:
func bindViewModel() {
self.tableView.delegate = nil
self.tableView.dataSource = nil
let emptyTrigger = searchBar
.rx.text.orEmpty
.filter { $0.isEmpty }
.throttle(0.1, scheduler: MainScheduler.instance)
.asDriver(onErrorJustReturn: "")
let loadMealTrigger = Observable
.of(emptyTrigger.asObservable(), Observable.just(("")))
.merge()
.asDriver(onErrorJustReturn: "")
let searchTrigger = searchBar.rx.text.orEmpty.asDriver()
.distinctUntilChanged()
.filter {!$0.isEmpty }
.throttle(0.1)
let selectMealTrigger = tableView.rx.itemSelected.asDriver()
let input = HomeViewModel.Input(
loadTrigger: loadMealTrigger,
searchTrigger: searchTrigger,
selectMealTrigger: selectMealTrigger
)
let output = viewModel.transform(input)
output.mealList
.drive(tableView.rx.items(cellIdentifier: MealCell.cellIdentifier)) { index, meal, cell in
let mealCell = cell as! MealCell
mealCell.meal = meal
}
.disposed(by: bag)
output.selectedMeal
.drive()
.disposed(by: bag)
}
Firstly, is this RxSwift?
If so, the .do(onNext:) operator provides side effects when you receive a new event via a subscription; Therefore, two "reactions" will happen when a table row is tapped: 1. subscription method and 2. .do(onNext:) event. Unfortunately, I do not have any further insight into your code, so there may be other stuff creating that error aswell.
Good luck!
Related
I want to show the contents of the collection view through search. However, there are many errors while binding collection views. I think the model code got weird first, where is it weird? How can we fix this error?
output.loadData.map(CollectionOfOne.init).bind(to: collectionView.rx.items) { (row,collectionView, element) -> UICollectionViewCell in
let indexPath = IndexPath(index: 1)
guard let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "friendCell", for: indexPath) as? FriendCell else {
return FriendCell()
}
cell.profileImage?.image = UIImage(systemName: element.image)
cell.nickNameLbl?.text = element.nickname
cell.listenBtn?.rx.tap.subscribe(onNext: {[unowned self] in
selectIndexPath.accept(row)
}).disposed(by: cell.disposeBag)
return cell
}
output.searchNameResult.map(CollectionOfOne.init).bind(to: collectionView.rx.items) { collectionView, index, element -> UICollectionViewCell in
let indexPath = IndexPath(index: 0)
guard let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "freindCell", for: indexPath) as? FriendCell else {
return FriendCell()
}
// cell.profileImage?.image = UIImage(systemName: (element as AnyObject).image)
cell.profileImage?.image = UIImage(named: element.img)
cell.nickNameLbl?.text = (element.nickname)
return cell
}
This is viewcontroller code
struct input {
let loadData: Signal<Void>
let searchName: Driver<Void>
let searchHashTag: Driver<Void>
}
struct output {
let loadData: BehaviorRelay<[User]>
let searchNameResult: PublishRelay<users>
}
func transform(_ input: input) -> output {
let api = SearchAPI()
let loadData = BehaviorRelay<[User]>(value: [])
let searchName = PublishRelay<users>()
input.loadData.asObservable().subscribe(onNext: { [weak self] in
guard let self = self else { return }
api.getFriend().subscribe(onNext: { (response, statuscode) in
switch statuscode {
case .ok:
// loadData.accept(response!, User)
result.onCompleted()
default:
result.onNext("default")
}
}).disposed(by: self.disposeBag)
}).disposed(by: disposeBag)
input.searchName.asObservable().withLatestFrom(input.name).subscribe(onNext: {[weak self] nickname in
guard let self = self else { return }
api.getUserList(nickname).subscribe(onNext: { (response,statuscode) in
switch statuscode {
case .ok:
result.onCompleted()
case .noHere:
result.onNext("no user")
default:
result.onNext("default")
}
}).disposed(by: self.disposeBag)
}).disposed(by: disposeBag)
And this is my viewmodel.
What is the problem?
You are getting the error: Incorrect argument label in call (have 'named:', expected 'systemName:') because of this other error: Value of type 'users' has no member 'img'.
I expect that you don't really want to display a single cell but the code is so muddled in general that it's hard to make out exactly what you are trying to do. I suggest you find yourself a tutor who can teach you about types, or possibly use the Swift Playgrounds app to learn the basics of how to code before you try to make a real program.
I have a ViewController with a UICollectionView and its elements are bound and cells created via:
self.viewModel.profileItems.bind(to: self.collectionView.rx.items){ (cv, row, item) ...
I also react to the user taps via:
self.collectionView.rx.modelSelected(ProfileItem.self).subscribe(onNext: { (item) in
if(/*special item*/) {
let xVC = self.storyboard?.instantiateViewController(identifier: "x") as! XViewController
xVC.item = item
self.navigationController?.pushViewController(xVC, animated: true)
} else {
// other generic view controller
}
}).disposed(by: bag)
The property in the xViewController for item is of Type ProfileItem?. How can changes to item in the XViewController be bound to the collectionView cell?
Thanks in advance
Your XViewController needs an Observable that emits a new item at the appropriate times... Then realize that this observable can affect what profileItems, or at least your view model, emits.
let xResult = self.collectionView.rx.modelSelected(ProfileItem.self)
.filter { /*special item*/ }
.flatMapFirst { [unowned self] (specialItem) -> Observable<Item> in
let xVC = self.storyboard?.instantiateViewController(identifier: "x") as! XViewController
xVC.item = item
self.navigationController?.pushViewController(xVC, animated: true)
return xVC.observableX // this needs to complete at the appropriate time.
}
self.collectionView.rx.modelSelected(ProfileItem.self)
.filter { /*not special item*/ }
.bind(onNext: { [unowned self] item in
// other generic view controller
}
.disposed(by: bag)
Now you need to feed xResult into your view model.
I've written module based on RxSwift with Viewcontroller and ViewModel. ViewModel contains gesture's observers and images observables. Everything works well, except situation when application didBecameActive directly to mentioned module. Subscriptions of gestures don't work and imageView become blank.
They are set inside subscription to observable based on BehaviorSubjects, inside view:
func subscribePhotos(observerable: Observable<[(Int, UIImage?)]>) {
disposeBag = DisposeBag()
observerable.subscribeOnNext { [weak self] array in
array.forEach { identifier, image in
if let pictureView = self?.subviews.first(where: { view -> Bool in
guard let view = view as? PictureView else {
return false
}
return view.identifier == identifier
}) as? PictureView {
pictureView.set(image)
}
}
}.disposed(by: disposeBag)
}
In viewModel I set Observable:
var imagesObservable: Observable<[(Int, UIImage?)]> {
do {
let collection = try photosSubject.value()
if let photosObservables = collectionCreator?.getPhotosDetailsObservables(identifiers: collection.photoIdentifiers) {
let photosObservable = Observable.combineLatest(photosObservables)
return Observable.combineLatest(photosSubject, photosObservable,
resultSelector: { collection, currentArray -> [(Int, UIImage?)] in
var newArray = [(Int, UIImage?)]()
currentArray.forEach { stringIdentifier, image in
if let picture = grid.pictures.first(where: { $0. stringIdentifier == stringIdentifier }) {
newArray.append((picture.identifier, image))
}
}
return newArray
})
}
} catch { }
return Observable<[(Int, UIImage?)]>.never()
}
}
photosSubject is initialized in viewModel's init
photosSubject = BehaviorSubject<PictureCollection>(value: collection)
photosObservale
func createImageObservableForAsset(asset: PHAsset, size: CGSize) -> Observable<UIImage?> {
return Observable.create { obs in
PHImageManager.default().requestImage(for: asset,
targetSize: size,
contentMode: .aspectFit,
options: nil,
resultHandler: { image, _ in
obs.onNext(image)
})
return Disposables.create()
}
}
And in ViewController I connect them by calling method of view:
myView.pictureView.subscribePhotos(observerable: viewModel.imagesObservable)
After didBecameActive pictureView's property image of type UIImage isn't nil, but they disappear. I could listen notification didBecameActive and invoke onNext on observer, but I’m not sure is it correct way to figure out problem. Any idea what's reason of that?
Finally, I solved out this issue. Reason wasn't connected with Rx. Method drawing pictures draw(_:CGRect) was called after didBecomeActive and cleared myView. I changed method's body and now everything works well :)
I have the following MVVM-C + RxSwift code.
The problem is that the TableView is not receiving any signals. When I
debug the results I can see that the API call is returning what it should, the objects array is populated with objects but the tableview does not show any results. Here is the console output:
2018-11-13 16:12:08.107: searchText -> Event next(qwerty)
Search something: qwerty
2018-11-13 16:12:08.324: viewModel.data -> Event next([])
Could it be the tableview itself? Maybe wrong custom cell setup?
ViewController.swift:
tableView = UITableView(frame: self.view.frame)
tableView.delegate = nil
tableView.dataSource = nil
tableView.register(SearchResultCell.self, forCellReuseIdentifier: "SearchResultCell")
viewModel.data
.debug("viewModel.data", trimOutput: false)
.drive(tableView.rx.items(cellIdentifier: "SearchResultCell")) { row, object, cell in
cell.name.text = object.name
cell.something.text = object.something
}
.disposed(by: disposeBag)
ViewModel.swift:
let disposeBag = DisposeBag()
var searchText = BehaviorRelay(value: "something to search for")
lazy var data: Driver<[Object]> = {
return self.searchText.asObservable()
.debug("searchText", trimOutput: false)
.throttle(0.3, scheduler: MainScheduler.instance)
.distinctUntilChanged()
.flatMapLatest(searchSomething)
.asDriver(onErrorJustReturn: [])
}()
func searchSomething(query: String) -> Observable<[Object]> {
print("Search something: \(query)")
let provider = MoyaProvider<APIService>()
var objects = [Object]()
provider.rx.request(.search(query: query)).subscribe { event in
switch event {
case let .success(response):
do {
let responseJSON: NSDictionary = try (response.mapJSON() as? NSDictionary)!
objects = self.parse(json: responseJSON["results"] as Any)
} catch(let error) {
print(error)
}
break
case let .error(error):
print(error)
break
}
}
.disposed(by: disposeBag)
let result: Observable<[Object]> = Observable.from(optional: objects)
return result
}
When using flatMap, you do not want to create nested subscriptions. You will create an Observable that returns the expected result, and flatMap will take care of subscribing to it. In the current state of things, searchSomething will always return an empty array, as Observable.from(optional: objects) will be called before the request has a chance to complete.
Since version 10.0 of Moya, provider will cancel the requests it created when deallocated. Here, it will be deallocated when execution exits searchSomething, hence the network request won't have time to finish. Moving provider's declaration to the view model's level solves this issue.
Here's searchSomething(query: String) -> Observable<[Object]> rewritten.
let provider = MoyaProvider<APIService>()
func searchSomething(query: String) -> Observable<[Object]> {
print("Search something: \(query)")
return provider.rx.request(.search(query: query)).map { (response) -> [Object] in
let responseJSON: NSDictionary = try (response.mapJSON() as? NSDictionary)!
return self.parse(json: responseJSON["results"] as Any)
}
}
Instead of doing the transformation in subscribe, it's done in map, which will be called for every next event, being passed the value associated with the event.
I have following code:
let categoriesRequest = APIService.MappableRequest(Category.self, resource: .categories)
let categoriesRequestResult = api.subscribeArrayRequest(categoriesRequest, from: actionRequest, disposedBy: disposeBag)
let newCategories = categoriesRequestResult
.map { $0.element }
.filterNil()
let categoriesUpdateData = DatabaseService.UpdateData(newObjectsObservable: newCategories)
let categoriesDatabaseResult = database.subscribeUpdates(from: categoriesUpdateData, disposedBy: disposeBag)
let latestTopicsRequest = APIService.MappableRequest(Topic.self, resource: .latestTopics)
let latestTopicsRequestResult = api.subscribeArrayRequest(latestTopicsRequest, from: actionRequest, disposedBy: disposeBag)
let newLastTopics = latestTopicsRequestResult
.map { $0.element }
.filterNil()
let latestTopicsUpdateData = DatabaseService.UpdateData(newObjectsObservable: newLastTopics)
let latestTopicsDatabaseResult = database.subscribeUpdates(from: latestTopicsUpdateData, disposedBy: disposeBag)
There are two requests that starts from the same publish subject actionRequest, and two database updates after these requests.
I need something like isActive, bool value which will return true if any of api/database task is in progress. Is it possible? I saw ActivityIndicator in RxSwift examples, but I don't know is it possible to use it in my case.
Code from api/database if needed:
// API
func subscribeArrayRequest<T>(_ request: MappableRequest<T>,
from triggerObservable: Observable<Void>,
disposedBy disposeBag: DisposeBag) -> Observable<Event<[T]>> {
let result = ReplaySubject<Event<[T]>>.create(bufferSize: 1)
triggerObservable
.flatMapLatest {
SessionManager
.jsonArrayObservable(with: request.urlRequest, isSecured: request.isSecured)
.mapResponse(on: APIService.mappingSheduler) { Mapper<T>().mapArray(JSONArray: $0) }
.materialize()
}
.subscribe(onNext: { [weak result] event in
result?.onNext(event)
})
.disposed(by: disposeBag)
return result
}
// Database
func subscribeUpdates<N, P>(from data: UpdateData<N, P>, disposedBy disposeBag: DisposeBag) -> Observable<Void> {
let result = PublishSubject<Void>()
data.newObjectsObservable
.observeOn(DatabaseService.writingSheduler)
.subscribe(onNext: { [weak result] newObjects in
// update db
DispatchQueue.main.async {
result?.onNext(())
}
})
.disposed(by: disposeBag)
return result
}
Thanks.
Found following solution: pass ActivityIndicator instance to services methods and use it like this:
func bindArrayRequest<T>(_ request: MappableRequest<T>,
from triggerObservable: Observable<Void>,
trackedBy indicator: RxActivityIndicator,
disposedBy disposeBag: DisposeBag) -> Observable<Event<[T]>> {
let result = ReplaySubject<Event<[T]>>.create(bufferSize: 1)
triggerObservable
.flatMapLatest {
SessionManager
.jsonArrayObservable(with: request.urlRequest, isSecured: request.isSecured)
.mapResponse(on: APIService.mappingSheduler) { Mapper<T>().mapArray(JSONArray: $0) }
.materialize()
.trackActivity(indicator)
}
.bind(to: result)
.disposed(by: disposeBag)
return result
}
Also wrap database update logic into observable and use it with .trackActivity(indicator) into flatMapLatest like in api.
Then use api/database methods like this:
let indicator = RxActivityIndicator()
isUpdatingData = indicator.asObservable() // needed bool observable
let categoriesRequest = APIService.MappableRequest(Category.self, resource: .categories)
let categoriesRequestResult =
api.bindArrayRequest(categoriesRequest,
from: actionRequest,
trackedBy: indicator,
disposedBy: disposeBag)
let newCategories = categoriesRequestResult
.map { $0.element }
.filterNil()
let categoriesUpdateData = DatabaseService.UpdateData(newObjectsObservable: newCategories)
let categoriesDatabaseResult =
database.bindUpdates(from: categoriesUpdateData,
trackedBy: indicator,
disposedBy: disposeBag)
let latestTopicsRequest = APIService.MappableRequest(Topic.self, resource: .latestTopics)
let latestTopicsRequestResult =
api.bindArrayRequest(latestTopicsRequest,
from: actionRequest,
trackedBy: indicator,
disposedBy: disposeBag)
let newLastTopics = latestTopicsRequestResult
.map { $0.element }
.filterNil()
let latestTopicsUpdateData = DatabaseService.UpdateData(newObjectsObservable: newLastTopics)
let latestTopicsDatabaseResult =
database.bindUpdates(from: latestTopicsUpdateData,
trackedBy: indicator,
disposedBy: disposeBag)
isUpdatingData observable is what I needed.