Skip to content

withLatestFrom not working as expect #171

Open
@cuzv

Description

@cuzv
final class ViewModel {
  let nodes = CurrentValueSubject<[Int], Never>([])
  var cancel: AnyCancellable?

  init() {
    cancel = nodes
      .dropFirst()
      .filter {
        !$0.isEmpty
      }
      .withLatestFrom(nodes)
      .sink { [weak self] nodes in
        guard let self else { return }

        print("nodes.count: \(nodes.count)")
        print("self.nodes.value.count: \(self.nodes.value.count)")
        if nodes.count != self.nodes.value.count {
          print("WTF")
        }
      }

    nodes.send([1, 2, 3])
  }
}

If you run it multiple times, it might print WTF. I've used the Rx framework for many years and I find this behavior quite strange. Is this normal or abnormal?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions