Questions tagged [rx-swift]

RxSwift's intention is to enable easy composition of asynchronous operations and event/data streams. Use this tag only for questions that are specific to the RxSwift - Reactive Programming paradigm, or those that require code in the language. Use the related tags [ios], [osx], [apple-watch], [cocoa-touch], and [cocoa] for (language-agnostic) questions about the platforms or frameworks.

About Rx and RxSwift

Rx is a generic abstraction of computation expressed through Observable<Element> interface.

RxSwift is a Swift version of Rx.

It tries to port as many concepts from the original version as possible, but some concepts were adapted for more pleasant and performant integration with iOS/macOS environment.

Cross platform documentation can be found on ReactiveX.io.

Like the original Rx, its intention is to enable easy composition of asynchronous operations and event/data streams.

KVO observing, async operations and streams are all unified under abstraction of sequence. This is the reason why Rx is so simple, elegant and powerful.


Resources

2149 questions
15
votes
1 answer

RxSwift -- MainScheduler.instance vs MainScheduler.asyncInstance

What is the difference between using RxSwift's MainSchedule.instance and MainSchedule.asyncInstance within the context of observeOn?
udi80z
  • 153
  • 1
  • 4
15
votes
3 answers

How to bind rx_tap (UIButton) to ViewModel?

I have authorization controller with 2 UITextField properties and 1 UIButton. I want to bind my View to ViewModel but don't know how to to do it. This is my AuthorizatioVC.swift: class AuthorizationViewController: UIViewController { let disposeBag…
Marina
  • 1,177
  • 4
  • 14
  • 27
14
votes
2 answers

Purpose of Disposables.create() in RxSwift

I'm learning RxSwift and I've come across the following pattern when creating Observables: return Observable.create { observer in let disposable = Disposables.create() // Do some stuff with observer here return disposable } As far as…
mike
  • 2,073
  • 4
  • 20
  • 31
13
votes
3 answers

RxSwift map and flatMap difference

I can't understand the difference between map and flatMap In RxSwift. In the RxSwift playground examples and the books, flatMap is used as converting Observables which has inner Observable property. However I see flatMap being used directly on…
Meanteacher
  • 2,031
  • 3
  • 17
  • 48
13
votes
1 answer

Can I raise an Error from rxSwift map-Function

Can I raise an error from a map function or do I need to use flatMap? The error should be reported to onError. In RxJava I can simply throw an exception. How can I do the following (pseudo-)code observable.map( value -> {if (value.isIllegal)…
Benjamin Mesing
  • 4,075
  • 1
  • 18
  • 22
13
votes
2 answers

Observe array in Swift 3 using RxSwift

To create an observable array using RxSwift in Swift 2, I use to do this: [1, 2, 3].toObservable().subscribeNext { print($0) } But in Swift 3, it doesn't work anymore, I got this error: Value of type '[Int]' has no member 'toObservable' How can I…
pableiros
  • 14,932
  • 12
  • 99
  • 105
13
votes
1 answer

flatMap Not returning onCompleted

I have created below function with chaining of multiple observables however whatever I do it does not seem to call completed ? it only return the following: (facebookSignInAndFetchData()) -> subscribed (facebookSignInAndFetchData()) -> Event…
Peter Pik
  • 11,023
  • 19
  • 84
  • 142
13
votes
2 answers

Does a read only BehaviorSubject interface exist in RX and if not, is it a bad idea to make one?

Implementations of rx provide BehaviorSubject and Variable as mechanisms for modeling properties that change over time (a useful replacement for C# INotifyPropertyChanged). Generally these are exposed as Observable but it would be more…
Cargowire
  • 1,488
  • 10
  • 21
13
votes
3 answers

RxSwift: Return a new observable with an error

I have a function that return a Bool Observable depending if it was ok or not. func test() -> Observable { if everythingIsOk { return just(true) } return just(false) <- how can i here return a custom error to retrieve what…
Godfather
  • 4,040
  • 6
  • 43
  • 70
13
votes
3 answers

RxSwift: Use Zip with different type observables

I'm using RxSwift 2.0.0-beta How can I combine 2 observables of different types in a zip like manner? // This works [just(1), just(1)].zip { intElements in return intElements.count } // This doesn't [just(1), just("one")].zip {…
GDanger
  • 1,641
  • 2
  • 22
  • 35
12
votes
1 answer

Difference between `.drive()` and `.bind(to:)`

I am learning RxSwift at the moment. When would you use .drive(something) and when .bind(to: something)? Example: let disposeBag = DisposeBag() let isEnabled = BehaviorRelay(value: true) let button = UIButton() // what is the benefit of…
Dich1944
  • 141
  • 1
  • 6
12
votes
1 answer

RXSwift Not subscribing on Main Thread

I am trying to make several API calls and populate a Realm Database. Everything works fine. However when I try to run performSegue() on subscribe() method an exception is raised, informing that I can't do this on a background thread, which is…
Exprove
  • 1,301
  • 2
  • 18
  • 32
12
votes
2 answers

RxCocoa - Why doesn't PublishRelay have an asDriver() method?

On RxCocoa I was wondering why the PublishRelay doesn't have an asDriver() method like the BehaviorRelay ? Currently if I want to convert the publishRelay into a Driver, I have to specify what to return in case of error which seems weird given that…
Bioche
  • 458
  • 4
  • 11
12
votes
2 answers

Error "nil requires a contextual type" using Swift

I'd like to achieve the following in code: class MyService { let mySubject = BehaviorSubject(value: nil) //.... } Unfortunately, I get the "nil requires a contextual type" error. I want the subject to be "empty" till I actually…
breakline
  • 5,776
  • 8
  • 45
  • 84
12
votes
3 answers

RxSwift double mapping in tableView.rx.itemSelected

I want to get object from tableView.rx.itemSelected and after process it. This method return IndexPath, so I can map this value. But how to get object at index from the ViewModel? struct ViewModel { var items: Observable<[Item]> } Approximate I…
biloshkurskyi.ss
  • 1,358
  • 3
  • 15
  • 34