<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Combine",
  "identifier" : "/documentation/Combine/Publisher/collect(_:options:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAE7collect_7optionsAA10PublishersO13CollectByTimeVy_xqd__GAG0H16GroupingStrategyOy_qd__G_16SchedulerOptionsQyd__SgtAA0K0Rd__lF"
  },
  "title" : "collect(_:options:)"
}
-->

# collect(_:options:)

Collects elements by a given time-grouping strategy, and emits a single array of the collection.

```
func collect<S>(_ strategy: Publishers.TimeGroupingStrategy<S>, options: S.SchedulerOptions? = nil) -> Publishers.CollectByTime<Self, S> where S : Scheduler
```

## Parameters

`strategy`

The timing group strategy used by the operator to collect and publish elements.

`options`

Scheduler options to use for the strategy.

## Return Value

A publisher that collects elements by a given strategy, and emits a single array of the collection.

## Discussion

Use [`collect(_:options:)`](/documentation/Combine/Publisher/collect(_:options:)) to emit arrays of elements on a schedule specified by a [`Scheduler`](/documentation/Combine/Scheduler) and `Stride` that you provide. At the end of each scheduled interval, the publisher sends an array that contains the items it collected. If the upstream publisher finishes before filling the buffer, the publisher sends an array that contains items it received. This may be fewer than the number of elements specified in the requested `Stride`.

If the upstream publisher fails with an error, this publisher forwards the error to the downstream receiver instead of sending its output.

The example above collects timestamps generated on a one-second <doc://com.apple.documentation/documentation/Foundation/Timer> in groups (`Stride`) of five.

```
let sub = Timer.publish(every: 1, on: .main, in: .default)
    .autoconnect()
    .collect(.byTime(RunLoop.main, .seconds(5)))
    .sink { print("\($0)", terminator: "\n\n") }

// Prints: "[2020-01-24 00:54:46 +0000, 2020-01-24 00:54:47 +0000,
//          2020-01-24 00:54:48 +0000, 2020-01-24 00:54:49 +0000,
//          2020-01-24 00:54:50 +0000]"
```

> Note: When this publisher receives a request for `.max(n)` elements, it requests `.max(count * n)` from the upstream publisher.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)