Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withLatestFrom not working as expect #171

Open
cuzv opened this issue Aug 28, 2024 · 1 comment
Open

withLatestFrom not working as expect #171

cuzv opened this issue Aug 28, 2024 · 1 comment

Comments

@cuzv
Copy link

cuzv commented Aug 28, 2024

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?

@VannDK
Copy link

VannDK commented Oct 14, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants