I am trying to learn the RxSwift library
I have a code like this:
if data.checkAllIsOk()
{
[do things]
}
else
{
[show alert]
}
Now I need to update the data from the server before checking, so I modeled getData () that returns an Observable.
My current approach is this:
getData()
>- flatMap{ (data:Data) -> Observable<Bool> in
_=0 // workaround for type inference bugs
return just(data.checkAllIsOk())
}
>- subscribeNext{ (ok) -> Void in
if ok
{
[do the things]
}
else
{
[show the alert]
}
}
>- disposeBag.addDisposable()
It works (or it should, I still write it), but it feels wrong .. is there a more "reactive" way to do this? What are the most suitable operators?
Perhaps it returns an error for "false" and uses a catch block?
Update
Following the approach suggested by ssrobbi i, we divided 2 branches into 2 different subscribeNext and used a filter to select a positive or negative branch. This is the result:
let checkData=getData()
>- flatMap{ (data:Data) -> Observable<Bool> in
_=0
return just(data.checkAllIsOk())
}
>- shareReplay(1)
}
[...]
checkData
>- filter{ (ok) -> Bool in
ok == true
}
>- subscribeNext{ (_) -> Void in
[do the things]
}
>- disposeBag.addDisposable()
checkData
>- filter{ (ok) -> Bool in
ok == false
}
>- subscribeNext{ (_) -> Void in
[show the alert]
}
>- disposeBag.addDisposable()
, , ( !)
RxSwift slack shareReplay (1), getData() .