ReactiveCocoa / ReactiveSwift

Streams of values over time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

flatMap(.latest) and delayed inner SignalProducer

TimPapler opened this issue Β· comments

Hello!

The following:

SignalProducer<Int, Never>([ 1, 2, 3, 4 ])
    .flatMap(.latest) { i in
        SignalProducer(value: i)
            .delay(1, on: QueueScheduler.main)
}
    .startWithValues { value in
        print("A", value)
}

Prints nothing πŸ€”

vs

SignalProducer<Int, Never>([ 1, 2, 3, 4 ])
    .flatMap(.latest) { i in
        SignalProducer.empty
            .delay(1, on: QueueScheduler.main)
            .then(SignalProducer(value: i))
}
    .startWithValues { value in
        print("B", value)
}

prints B 4 πŸŽ‰

Why is this the case?

If you run those exact examples, you won't see the values from the queue scheduler because:

  1. You're not waiting for the value to get send on the main queue
  2. You may be blocking the main queue

The second example will work because the empty completes immediately, which doesn't require a delay and lets the then run immediately.

I made command line tool app with main.swift containing:

import Foundation
import ReactiveSwift

let scheduler = QueueScheduler(qos: .userInitiated)
SignalProducer<Int, Never>([ 1, 2, 3, 4 ])
	.flatMap(.latest) { i in
		SignalProducer(value: i).delay(1, on: scheduler)
	}
	.logEvents()
	.startWithCompleted {
		exit(1)
	}

dispatchMain()

in console i get:

[] starting fileName: /Users/tim/development/Lake2/Carthage/Checkouts/ReactiveSwift/test/main.swift, functionName: test, lineNumber: 17
[] started fileName: /Users/tim/development/Lake2/Carthage/Checkouts/ReactiveSwift/test/main.swift, functionName: test, lineNumber: 17
[] completed fileName: /Users/tim/development/Lake2/Carthage/Checkouts/ReactiveSwift/test/main.swift, functionName: test, lineNumber: 17
[] terminated fileName: /Users/tim/development/Lake2/Carthage/Checkouts/ReactiveSwift/test/main.swift, functionName: test, lineNumber: 17

This prints no value, but does complete. I don't think I am blocking scheduler and I am waiting for values. Sorry for insisting with this πŸ™ˆ

That does seem like it should work. πŸ€”

Hello. πŸ‘‹ Thanks for opening this issue. Due to inactivity, we will soft close the issue. If you feel that it should remain open, please let us know. πŸ˜„