メインコンテンツまでスキップ

Pull vs Push Subscription

UniTask v2 has support for Asynchronous LINQ. Asynchronous LINQ is an extension to IUniTaskAsyncEnumerable<T> and its usage can be very similar to UniRx, but the process behind it is different (UniRx is push-based while UniTask is pull-based).

Kassets' ScriptableObject also make use of Asynchronous LINQ. Kassets' ScriptableObject derived from IUniTaskAsyncEnumerable<T> so it is possible to directly apply various features of UniTask as explained in its github page or from this slide (Japanese).

When using pull-based subscriptions of UniTask.Linq, when the process of handling an event is still running, it won't be called again until it is over, no matter how many times the event has been raised during the process. Reversely, push-based subscriptions of UniRx will execute every event raise.

When both UniRx and UniTask are imported together, It can be confusing which of the Subscription behavior is in effect (pull-based or push-based?). To use Kassets' instance as IObservable, use AsObservable(). To use Kassets' instance as IUniTaskAsyncEnumerable use AsAsyncEnumerable(). Unless referenced by interface, Kassets instances Default Subscribe Behavior can be selected from the inspector window.

Screenshot 2023-06-12 at 16 49 31
注記

UniTask Asynchronous LINQ is part of Cysharp.Threading.Tasks.Linq namespace. To use, add UniTask.Linq as reference to your project's Assembly Definition.

注記

From v2.7.0, Upon importing R3, handling pull/push based can be done by converting Kassets instances using AsObservable() for push-based, and ToAsyncEnumerable() for pull-based.

Reference: Pull IAsyncEnumerable vs Push Observable