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

OR-5261 Combining Operators:: switchToLatest() #98

Merged
merged 1 commit into from
Jul 13, 2023
Merged

Conversation

crazymanish
Copy link
Owner

Context

Operators are publishers

  • In Combine, methods that perform an operation on values coming from a publisher are called operators.
  • Each Combine operator actually returns a publisher. Generally speaking, that publisher receives the upstream values, manipulates the data, and then sends that data downstream.

In this PR

  • Combining Operators:: switchToLatest()
  • switchToLatest() Republishes elements sent by the most recently received publisher.
  • https://developer.apple.com/documentation/combine/publishers/reduce/switchtolatest()
  • This operator works with an upstream publisher of publishers, flattening the stream of elements to appear as if they were coming from a single stream of elements. It switches the inner publisher as new ones arrive but keeps the outer publisher constant for downstream subscribers.
  • When this operator receives a new publisher from the upstream publisher, it cancels its previous subscription. Use this feature to prevent earlier publishers from performing unnecessary work, such as creating network request publishers from frequently updating user interface publishers.

  • If you’re not sure why this is useful in a real-life app, consider the following scenario:
  • Your user taps a button that triggers a network request. Immediately afterward, the user taps the button again, which triggers a second network request. But how do you get rid of the pending request, and only use the latest request? switchToLatest to the rescue!

@crazymanish crazymanish requested review from buh and raihan July 13, 2023 05:47
@crazymanish crazymanish linked an issue Jul 13, 2023 that may be closed by this pull request
@crazymanish crazymanish merged commit 0f4d735 into main Jul 13, 2023
@crazymanish crazymanish deleted the OR-5261-p4 branch July 13, 2023 05:47
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

Successfully merging this pull request may close these issues.

[w23(4june-10june)] Operators:: Combining Operators: Combining
1 participant