These are chat archives for ReactiveX/RxJava

12th
Aug 2015
Bartłomiej Hołota
@bholota
Aug 12 2015 14:50
I'm trying to understand defer(), should I use it in case when I'm creating Observable from list of Strings, map those into File and just after that subscribe to it? Should this observable be deffered or not?
Dorus
@Dorus
Aug 12 2015 15:00
Defer is for lazy evaluation. Use it if you need lazy evaluation.
Bartłomiej Hołota
@bholota
Aug 12 2015 15:04
Ok, so in cases when I want wait with emission until someone subscribe to it, right?
Dorus
@Dorus
Aug 12 2015 15:07
Yes, as normal for cold observables. There are multiple ways to accomplish that, and defer is one. I believe Observable.create() and many others can do the same.
Bartłomiej Hołota
@bholota
Aug 12 2015 18:55
ok, one more thing: when I will Observable.from(...).map(...).subscribe(...) and in such chaining this subscribe will catch all emitted items? It don't has to be defered? (it's hot observable right?)
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Aug 12 2015 22:43
  1. It's not hot observable
  1. subscriber will catch all items
  1. It needs to be deferred only if in from(something) something should be computed only after subscription to the observable
ah, sorry for indexes, markdown…
About defer: imagine you have Observable.just(networkRequest())
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Aug 12 2015 22:49
the problem is that in Java all arguments computed before execution of the function, so you can not schedule networkRequest() on the other thread, it will be computed in thread that called Observable.just(networkRequest()).
To avoid that you need to have a function that wraps your call and defer it:
Observable
  .defer(() -> Observable.just(networkRequest())
  .map()
  .subscribe()
this will be cold observable and networkRequest() will be executed only after subscription.