A type-erasing cancellable object that executes a provided closure when canceled.
Subscriber implementations can use this type to provide a “cancellation token” that makes it possible for a caller to cancel a publisher, but not to use the ``Subscription`` object to request items.
An ``AnyCancellable`` instance automatically calls ``Cancellable/cancel()`` when deinitialized.
final func store<C>(in collection: inout C) where C : RangeReplaceableCollection, C.Element == AnyCancellable
Stores this type-erasing cancellable instance in the specified collection.
- Parameter collection: The collection in which to store this ``AnyCancellable``.
Omit the output parameters from the call. The result contains $ret and $out, with collection inside $out.
method · iOS 13.0+
cancel
TypeScript
cancel(): void
Swift declaration
final func cancel()
Cancel the activity.
When implementing ``Cancellable`` in support of a custom publisher, implement `cancel()` to request that your publisher stop calling its downstream subscribers. Combine doesn't require that the publisher stop immediately, but the `cancel()` call should take effect quickly. Canceling should also eliminate any strong references it currently holds.
After you receive one call to `cancel()`, subsequent calls shouldn't do anything. Additionally, your implementation must be thread-safe, and it shouldn't block the caller.
> Tip: Keep in mind that your `cancel()` may execute concurrently with another call to `cancel()` --- including the scenario where an ``AnyCancellable`` is deallocating --- or to ``Subscription/request(_:)``.