RXSwift simple async example? - swift

I'm new in RXSwift, I just try to implement a simple async example
but subscribe will never be called.
What I miss ?
let disposeBag = DisposeBag()
Observable<Any>.create {
observer in
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 3) {
observer.onNext("done")
observer.onCompleted()
}
return Disposables.create()
}
.subscribe { print($0) }
.addDisposableTo(disposeBag)
================= UPDATE ======================
I'm trying to do something more advanced, a chain which allow pass value from previous, but the result was not expected. what I miss ?
output was
next(done 1 - done 2)
what I expected was
next(done 1)
next(done 1 - done 2)
completed
class AsyncObject {
func asyncTest1() -> Observable<String> {
return Observable<String>.create {
(o: AnyObserver<String>) -> Disposable in
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
o.onNext("done 1")
o.onCompleted()
}
return Disposables.create()
}
}
func asyncTest2(value: String) -> Observable<String> {
return Observable<String>.create {
(o: AnyObserver<String>) -> Disposable in
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
o.onNext("\(value) - done 2")
o.onCompleted()
}
return Disposables.create()
}
}
}
class ViewController: UIViewController {
let disposeBag = DisposeBag()
let observer = AsyncObject()
// MARK: Init Methods
override func viewDidLoad() {
super.viewDidLoad()
self.observer.asyncTest1()
.flatMap { self.observer.asyncTest2(value: $0) }
.subscribe { print($0) }
.addDisposableTo(disposeBag)
}
}

I think you can merge this with the original one, to achieve the expected result. So it should look something like this
let test1 = self.observer.asyncTest1()
let test2 = test1.flatMap { self.observer.asyncTest2() }
Observable
.of(test1, test2)
.merge()
.subscribe { print($0) }
.addDisposableTo(disposeBag)

got help from RxSwift Slack #sergdot,
let test1 = self.observer.asyncTest1().shareReplay(1)
let test2 = test1.flatMap { self.observer.asyncTest2(value: $0) }
Observable.of(test1, test2).merge().subscribe {
print($0)
}
or
let test1 = self.observer.asyncTest1().shareReplay(1)
let test2 = test1.flatMap { self.observer.asyncTest2(value: $0) }
test1.concat(test2).subscribe {
print($0)
}

Related

How to reset and start new timer when enter new email

I have an input that accept string, and store it in local data. My use case is when I enter an email, the user must wait for 2 minute for requesting send email verification. But when I enter a different email name, I can't make the timer reset when the last email I enter is still countdown. I'm using RxSwift for timer, I don't know how to invalidate the timer in RxSwift.
This is what I came so far to reset the timer when user enters new email
// function that accept email from uitextfield.text
func resendEmailCountdown(with email: String) {
if email != getLoggedEmail() {
startCountdown(countdown: 0)
startCountdown(countdown: 120)
} else {
startCountdown(countdown: 120)
}
}
private func startCountdown(countdown: Int) {
let counter = countdown
if counter == 0 {
_ = Observable<Int>.timer(.seconds(0), period: .seconds(1), scheduler: MainScheduler.instance)
.take(0)
.subscribe(onNext: { [weak self] countdown in
guard let self = self else { return }
}).disposed(by: disposeBag)
} else {
_ = Observable<Int>.timer(.seconds(0), period: .seconds(1), scheduler: MainScheduler.instance)
.take(counter + 1)
.subscribe(onNext: { [weak self] countdown in
guard let self = self else { return }
let count = counter - countdown
if count != 0 {
self.eventResendEmailCountdown.onNext(count)
self.eventShowHideResendEmailButton.onNext(false)
} else {
self.eventShowHideResendEmailButton.onNext(true)
self.eventDismissCountdownBottomSheet.onNext(())
}
}).disposed(by: disposeBag)
}
}
The key is using flatMapLatest to cancel the previous timer and start up a new one.
Based on your description, here is what you need:
struct Output {
let eventResendEmailCountdown: Observable<Int>
let eventShowHideResendEmailButton: Observable<Bool>
let eventDismissCountdownBottomSheet: Observable<Void>
}
func example(text: Observable<String?>) -> Output {
let trigger = text.share()
let eventResendEmailCountdown = trigger
.flatMapLatest { _ in
Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
.map { 120 - $0 }
.take(until: { $0 == -1 })
}
let eventShowHideResendEmailButton = Observable.merge(
trigger.map { _ in false },
eventResendEmailCountdown.filter { $0 == 0 }.map { _ in true }
)
let eventDismissCountdownBottomSheet = eventResendEmailCountdown
.filter { $0 == 0 }
.map { _ in }
return Output(
eventResendEmailCountdown: eventResendEmailCountdown,
eventShowHideResendEmailButton: eventShowHideResendEmailButton,
eventDismissCountdownBottomSheet: eventDismissCountdownBottomSheet
)
}
Call it something like this:
let output = example(text: textField.rx.text.asObservable())
output.eventResendEmailCountdown
.debug("eventResendEmailCountdown")
.subscribe()
output.eventShowHideResendEmailButton
.debug("eventShowHideResendEmailButton")
.subscribe()
output.eventDismissCountdownBottomSheet
.debug("eventDismissCountdownBottomSheet")
.subscribe()

How do I bind a ViewModel to a Collationview?

I'm trying to bind a view model to a collection view. But I don't know how to do it. I'm using MVVM pattern and RxSwift, and I've only tried table view binding before. Here's my view model and the view controller code I've done so far.
class SearchViewModel: ViewModelType {
private let disposeBag = DisposeBag()
struct input {
let loadData: Signal<Void>
}
struct output {
let result: Signal<String>
let loadApplyList: PublishRelay<friends>
}
func transform(_ input: input) -> output {
let api = SearchAPI()
let result = PublishSubject<String>()
let loadApplyList = PublishRelay<friends>()
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:
if let response = response {
loadApplyList.accept(response)
}
default:
print("default")
}
}).disposed(by: self.disposeBag)
}).disposed(by: disposeBag)
return output(result: result.asSignal(onErrorJustReturn: ""), loadApplyList: loadApplyList)
}
}
This is my ViewModel code
func bindViewModel() {
let input = SearchViewModel.input(loadData: loadData.asSignal(onErrorJustReturn: ()))
let output = viewModel.transform(input)
}
And this is my ViewController code.
How should the collection view bind?
Here is what your view model should look like:
class SearchViewModel {
// no need for a disposedBag. If you are putting a disposeBag in your view model, you are likely doing something wrong.
struct input {
let loadData: Signal<Void>
}
struct output {
let loadApplyList: Driver<[User]> // you should be passing an array here, not an object.
}
func transform(_ input: input) -> output {
let api = SearchAPI()
let friendResult = input.loadData
.flatMapLatest {
api.getFriend()
.compactMap { $0.0.map(Result<friends, Error>.success) }
.asDriver(onErrorRecover: { Driver.just(Result<friends, Error>.failure($0)) })
}
let loadApplyList = friendResult
.compactMap { (result) -> [User]? in
guard case let .success(list) = result else { return nil }
return list.friends
}
return output(loadApplyList: loadApplyList)
}
}
Now in your view controller, you can bind it like this:
func bindViewModel() {
let input = SearchViewModel.input(loadData: loadData.asSignal(onErrorJustReturn: ()))
let output = viewModel.transform(input)
output.loadApplyList
.drive(collectionView.rx.items(cellIdentifier: "Cell", cellType: MyCellType.self)) { index, item, cell in
// configure cell with item here
}
.disposed(by: disposeBag)
}

RxSwift withLatestFrom with resultSelector doesn't compile

I have a Driver of type Bool and a BehaviorRelay of type Page (which is a custom enum).
enum Page {
case option1(CustomClass1, CustomClass2)
case option2(CustomClass3)
case option3(CustomClass4)
var property1: CustomClass2? {
switch self {
case .option1(_, let custom):
return custom
case .option2, .option3:
return nil
}
}
}
I have the Driver<Bool> in another ViewModel.
class ViewModel1 {
struct Output {
let hasItems: Driver<Bool>
}
let output: Output
init() {
let hasItemsRelay: BehaviorRelay<Bool> = BehaviorRelay<Bool>(value: false)
self.output = Output(
hasItems: hasItemsRelay.asDriver()
)
}
}
And I have a BehaviorRelay<Page?> in my base class.
class ViewModel2 {
let currentPageRelay: BehaviorRelay<Page?> = BehaviorRelay<Page?>(value: nil)
init() {
self.currentPageRelay = BehaviorRelay<Page?>(value: nil)
}
}
In ViewModel2 class I'm trying to catch an event on the hasItems driver of ViewModel1.Input and when I get an event, I need the current value of currentPageRelay and later on do stuff with it. So basically withLatestFrom is the thing I need to use.
class ViewModel2 {
private func test() {
let customViewModel: ViewModel1 = ViewModel1()
customViewModel
.output
.hasItems
.withLatestFrom(currentPageRelay) { ($0, $1) }
.map { (hasItems, page) -> (CustomClass2, Bool)? in
guard let property1 = page?.property1 else { return nil }
return (property1, hasItems)
}
.unwrap()
.drive(onNext: { (property1, hasItems) in
// do stuff
}
.disposed(by: disposeBag)
}
}
Xcode completely loses it on the withLatestFrom. No code completion and it gives the following compile error:
Expression type '(Bool, _)' is ambiguous without more context
I'm completely in the dark about this one. I've already tried everything, providing the correct classes in the parameter list below it, so that it knows what to expect etc, but no luck so far.
I think because .withLatestFrom requires both types it operates on to be of the same observable trait. So both should be either Observable, Driver, Signal, etc.
If you want to keep your Driver in your viewModel a Driver you could add an .asObservable() after the .hasItems:
class ViewModel2 {
let currentPageRelay: BehaviorRelay<Page?> = BehaviorRelay<Page?>(value: nil)
let disposeBag = DisposeBag()
init() {
// self.currentPageRelay = BehaviorRelay<Page?>(value: nil)
}
private func test() {
let customViewModel: ViewModel1 = ViewModel1()
customViewModel
.output
.hasItems
.asObservable()
.withLatestFrom(currentPageRelay) { ($0, $1) }
.map { (hasItems, page) -> (CustomClass2, Bool)? in
guard let property1 = page?.property1 else { return nil }
return (property1, hasItems)
}
.asDriver(onErrorJustReturn: nil)
.drive(onNext: {
guard let (property1, hasItems) = $0 else {
return
}
// do stuff
})
.disposed(by: disposeBag)
}
}
Or add a .asDriver() to currentPageRelay in the withLatestFrom(..):
customViewModel
.output
.hasItems
.withLatestFrom(currentPageRelay.asDriver()) { ($0, $1) }
.map { (hasItems, page) -> (CustomClass2, Bool)? in
guard let property1 = page?.property1 else { return nil }
return (property1, hasItems)
}
.drive(onNext: {
guard let (property1, hasItems) = $0 else {
return
}
// do stuff
})
.disposed(by: disposeBag)

RxSwift and MVVM: observable not executing without binding

I'm new to RxSwift and trying implement app that using MVVM architecture. I have view model:
class CategoriesViewModel {
fileprivate let api: APIService
fileprivate let database: DatabaseService
let categories: Results<Category>
// Input
let actionRequest = PublishSubject<Void>()
// Output
let changeset: Observable<(AnyRealmCollection<Category>, RealmChangeset?)>
let apiSuccess: Observable<Void>
let apiFailure: Observable<Error>
init(api: APIService, database: DatabaseService) {
self.api = api
self.database = database
categories = database.realm.objects(Category.self).sorted(byKeyPath: Category.KeyPath.name)
changeset = Observable.changeset(from: categories)
let requestResult = actionRequest
.flatMapLatest { [weak api] _ -> Observable<Event<[Category]>> in
guard let strongAPI = api else {
return Observable.empty()
}
let request = APIService.MappableRequest(Category.self, resource: .categories)
return strongAPI.mappedArrayObservable(from: request).materialize()
}
.shareReplayLatestWhileConnected()
apiSuccess = requestResult
.map { $0.element }
.filterNil()
.flatMapLatest { [weak database] newObjects -> Observable<Void> in
guard let strongDatabase = database else {
return Observable.empty()
}
return strongDatabase.updateObservable(with: newObjects)
}
apiFailure = requestResult
.map { $0.error }
.filterNil()
}
}
and I have following binginds in view controller:
viewModel.apiSuccess
.map { _ in false }
.bind(to: refreshControl.rx.isRefreshing)
.disposed(by: disposeBag)
viewModel.apiFailure
.map { _ in false }
.bind(to: refreshControl.rx.isRefreshing)
.disposed(by: disposeBag)
But if I comment bindings, part with database updating stops executing. I need to make it execute anyway, without using dispose bag in the view model. Is it possible?
And little additional question: should I use weak-strong dance with api/database and return Observable.empty() like in my view model code or can I just use unowned api/unowned database safely?
Thanks.
UPD:
Function for return observable in APIService:
func mappedArrayObservable<T>(from request: MappableRequest<T>) -> Observable<[T]> {
let jsonArray = SessionManager.jsonArrayObservable(with: request.urlRequest, isSecured: request.isSecured)
return jsonArray.mapResponse(on: mappingSheduler, { Mapper<T>().mapArray(JSONArray: $0) })
}
Work doesn't get done unless there is a subscriber prepared to receive the results.
Your DatabaseService needs to have a dispose bag in it and subscribe to the Observable<[Category]>. Something like:
class ProductionDatabase: DatabaseService {
var categoriesUpdated: Observable<Void> { return _categories }
func updateObservable(with categories: Observable<[Category]>) {
categories
.subscribe(onNext: { [weak self] categories in
// store categories an then
self?._categories.onNext()
})
.disposed(by: bag)
}
private let _categories = PublishSubject<Void>()
private let bag = DisposeBag()
}
Then apiSuccess = database.categoriesUpdated and database.updateObservable(with: requestResult.map { $0.element }.filterNil())

Rx flatMapLatest doesn't pass completed through?

I expected to see timer -> Event completed get outputted right after I saw running -> Event completed but it didn't happen.
Can someone explain why and give me some idea on how to complete the timer observable?
/// playground
import RxSwift
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
struct TimerCountdown {
let timer: Observable<Int>
init(running: Observable<Bool>) {
timer = running.flatMapLatest { $0 ? Observable<Int>.interval(1.0, scheduler: MainScheduler.instance) : Observable<Int>.never() }
}
}
let running = PublishSubject<Bool>()
let countdown = TimerCountdown(running: running)
_ = running.debug("running").subscribe()
_ = countdown.timer.debug("timer").subscribe()
running.onNext(true)
DispatchQueue.main.asyncAfter(deadline: .now() + 3.5) {
running.onNext(false)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 7.5) {
running.onNext(true)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 10.5) {
running.onCompleted()
}
This is the solution I found:
struct TimerCountdown {
let timer: Observable<Int>
init(running: Observable<Bool>) {
timer = running
.flatMapLatest { $0 ? Observable<Int>.interval(1.0, scheduler: MainScheduler.instance) : Observable<Int>.never() }
.takeUntil(running.materialize().filter { $0.isCompleted })
}
}
Here's another answer that doesn't require subscribing to running twice:
struct TimerCountdown {
let timer: Observable<Int>
init(running: Observable<Bool>) {
timer = running.materialize()
.flatMapLatest { (event) -> Observable<Int> in
switch event {
case let .next(value):
return value ? Observable<Int>.interval(1.0, scheduler: MainScheduler.instance) : Observable.never()
case .completed:
return Observable.empty()
case let .error(error):
return Observable.error(error)
}
}
}
}