Short description of the issue:
When trying to use the reduce method from Variable.asObservable() it doesn't seem to call the property or method in the subscribe method
Expected outcome:
To be consistent with Observable object and return the value on subscribe with it calling the method and property
What actually happens:
it doesn't seem to call the property or method in the subscribe method.
Self contained code example that reproduces the issue:
import XCTest
@testable import RxSwift
class VariableReduceTest: XCTestCase {
let dispose = DisposeBag()
func testVariableCanReduce() {
let expectation = XCTestExpectation(description: "Reduce from Variable")
var myText: String? {
didSet {
print("Set: \(myText)")
}
}
let variable = Variable(0)
let observable = variable.asObservable()
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: { variable.value = 1 })
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: { variable.value = 2 })
observable
.debug()
.reduce("", accumulator: { (text, int) in
return "ABC"
})
.subscribe(onNext: { newValue in
print("Value: \(newValue)")
// it assign value here
myText = newValue
// all pass here
XCTAssertNotNil(myText)
XCTAssertTrue(myText?.isEmpty == false)
XCTAssertTrue(myText == "ABC")
expectation.fulfill()
})
.disposed(by: dispose)
// myText fails all test here
XCTAssertNotNil(myText)
XCTAssertTrue(myText?.isEmpty == false)
XCTAssertTrue(myText == "ABC")
// filfill doesn't get called here
wait(for: [expectation], timeout: 5.0)
}
}
RxSwift/RxCocoa/RxBlocking/RxTest version/commit
4.1.2 and also used master
Platform/Environment
How easy is to reproduce? (chances of successful reproduce after running the self contained code)
Xcode version:
9.2
Hi @shams-ahmed ,
reduce emits only one element when sequence completes. You probably want scan operator.
Most helpful comment
Hi @shams-ahmed ,
reduceemits only one element when sequence completes. You probably wantscanoperator.