Pol
Pol

Reputation: 481

Evaluate startwith's param when subscribing

observableEmitingItemsRegularly
.map(this::cacheLastItem) //set the object in lastItem variable
.startWith(lastItem);

Returns always the same object at the beginning: The value interpreted at the time that line got interpreted.

I already tried:

Changing

.startWith(lastItem);

with

.startWith(getLastItem());

The function getLastItem() is called when we initialize the observable, not when we subscribe as I want to.

Many Thanks !

Upvotes: 2

Views: 103

Answers (1)

LordRaydenMK
LordRaydenMK

Reputation: 13321

Have you tried using Defer

From the documentation:

Defer do not create the Observable until the observer subscribes, and create a fresh Observable for each observer

.startWith(Observable.defer(() -> Observable.from(getLastItem()));

Upvotes: 2

Related Questions