CombineCommunity / CombineExt

CombineExt provides a collection of operators, publishers and utilities for Combine, that are not provided by Apple themselves, but are common in other Reactive Frameworks and standards.
https://combine.community
MIT License
1.72k stars 151 forks source link

More efficient collect by time/scheduler? #127

Open jessegrosjean opened 2 years ago

jessegrosjean commented 2 years ago

Thanks for CombineExt, I've been using in my apps for a long time.

I recently ran into a performance issue with "Combine" .collect(.byTime). It starts a repeating timer that runs for the entire duration of the subscription. Depending on the time stride and how many of these you have it can really effect performance even when no items are getting published.

The problem that I was trying to solve with .collect(.byTime) is:

  1. You have background work being performed and generating a sequence of results.
  2. You have a UI that you want to update with those results (you want them all, not just last/first)
  3. You want to batch process the results, it's too expensive to schedule each result separately on the main queue.
  4. You only want to schedule a timer when items are passing through the publisher, don't want a repeating timer

If there's a good Combine or CombineExt way to do this I would love to know. Otherwise I have a publisher that does what I need here. The timing logic is quite simple, though I don't really understand the details of the surrounding "Combine" logic. It's mostly copied/pasted from CombineX.

https://forums.swift.org/t/combines-collect-bytime-schedules-a-repeating-timer/57456

Assuming this type of scheduler doesn't already existing I think the basic logic would be a good addition to CombineExt, though probably someone more in tune with combine internals should check and adapt the code to CombineExt.