Search Results for

    Show / Hide Table of Contents

    Class Batcher

    Provides utility methods for processing items in batches with support for callbacks and error handling.

    Inheritance
    System.Object
    Batcher
    Inherited Members
    System.Object.ToString()
    System.Object.Equals(System.Object)
    System.Object.Equals(System.Object, System.Object)
    System.Object.ReferenceEquals(System.Object, System.Object)
    System.Object.GetHashCode()
    System.Object.GetType()
    System.Object.MemberwiseClone()
    Namespace: UniUtils.Data
    Assembly: cs.temp.dll.dll
    Syntax
    public static class Batcher

    Methods

    ProcessInBatches<T>(IReadOnlyList<T>, Action<T>, Int32, Single, Action<T>, Action<T, Boolean>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>, Action<T, Exception>)

    Executes an action on a list of items in batches, with optional callbacks for various stages of processing.

    Declaration
    public static IEnumerator ProcessInBatches<T>(IReadOnlyList<T> itemsList, Action<T> actionOnItem, int itemsPerBatch = 5, float batchDelay = 0.1F, Action<T> onItemProcessStart = null, Action<T, bool> onItemProcessFinished = null, Action<IReadOnlyList<T>> onBatchStart = null, Action<IReadOnlyList<T>> onBatchFinished = null, Action<IReadOnlyList<T>> onFinished = null, Action<T, Exception> onError = null)
    Parameters
    Type Name Description
    IReadOnlyList<T> itemsList

    The list of items to process.

    Action<T> actionOnItem

    The action to execute on each item.

    System.Int32 itemsPerBatch

    The number of items to process per batch. Defaults to 5.

    System.Single batchDelay

    The delay (in seconds) between processing batches. Defaults to 0.1f.

    Action<T> onItemProcessStart

    Optional callback invoked before processing each item; receives the item as T.

    Action<T, System.Boolean> onItemProcessFinished

    Optional callback invoked after processing each item; receives the item as T and a bool indicating success.

    Action<IReadOnlyList<T>> onBatchStart

    Optional callback invoked before processing each batch; receives the batch as IReadOnlyList<T>.

    Action<IReadOnlyList<T>> onBatchFinished

    Optional callback invoked when a batch is completed; receives the batch as IReadOnlyList<T>.

    Action<IReadOnlyList<T>> onFinished

    Optional callback invoked when all items have been processed; receives the full list of processed items as IReadOnlyList<T>.

    Action<T, Exception> onError

    Optional callback invoked when an error occurs during item processing; receives the item that failed (T) and the thrown Exception.

    Returns
    Type Description
    IEnumerator

    An enumerator that can be used to execute the batches over time.

    Type Parameters
    Name Description
    T

    The type of items in the list.

    Examples
    IReadOnlyList<int> numbers = new IReadOnlyList<int>() { 1, 2, 3, 4, 5, 6, 7 };
    
    StartCoroutine(Batcher.ProcessInBatches(
        numbers,
        n => Debug.Log($"Processing: {n}"),
        itemsPerBatch: 3,
        batchDelay: 0.5f,
        onBatchStart: batch => Debug.Log($"Batch starting: {string.Join(", ", batch)}"),
        onBatchFinished: batch => Debug.Log($"Batch finished: {string.Join(", ", batch)}"),
        onFinished: all => Debug.Log("All items processed.")
    ));
    Exceptions
    Type Condition
    System.ArgumentNullException

    Thrown if actionOnItem is null.

    ProcessPreBatched<T>(IReadOnlyList<IReadOnlyList<T>>, Action<T>, Single, Action<T>, Action<T, Boolean>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>, Action<T, Exception>)

    Executes an action on pre-batched lists of items, with optional callbacks for various stages of processing.

    Declaration
    public static IEnumerator ProcessPreBatched<T>(IReadOnlyList<IReadOnlyList<T>> preBatchedLists, Action<T> actionOnItem, float batchDelay = 0.1F, Action<T> onItemProcessStart = null, Action<T, bool> onItemProcessFinished = null, Action<IReadOnlyList<T>> onBatchStart = null, Action<IReadOnlyList<T>> onBatchFinished = null, Action<IReadOnlyList<T>> onFinished = null, Action<T, Exception> onError = null)
    Parameters
    Type Name Description
    IReadOnlyList<IReadOnlyList<T>> preBatchedLists

    The pre-batched lists of items to process.

    Action<T> actionOnItem

    The action to execute on each item.

    System.Single batchDelay

    The delay (in seconds) between processing batches. Defaults to 0.1f.

    Action<T> onItemProcessStart

    Optional callback invoked before processing each item; receives the item as T.

    Action<T, System.Boolean> onItemProcessFinished

    Optional callback invoked after processing each item; receives the item as T and a bool indicating success.

    Action<IReadOnlyList<T>> onBatchStart

    Optional callback invoked before processing each batch; receives the batch as IReadOnlyList<T>.

    Action<IReadOnlyList<T>> onBatchFinished

    Optional callback invoked when a batch is completed; receives the batch as IReadOnlyList<T>.

    Action<IReadOnlyList<T>> onFinished

    Optional callback invoked when all items have been processed; receives the full list of processed items as IReadOnlyList<T>.

    Action<T, Exception> onError

    Optional callback invoked when an error occurs during item processing; receives the item that failed (T) and the thrown Exception.

    Returns
    Type Description
    IEnumerator

    An enumerator that can be used to execute the batches over time.

    Type Parameters
    Name Description
    T

    The type of items in the batches.

    Examples
    IReadOnlyList<IReadOnlyList<int>> batches = new IReadOnlyList<IReadOnlyList<int>>()
    {
        new IReadOnlyList<int>() { 1, 2, 3 },
        new IReadOnlyList<int>() { 4, 5, 6 },
        new IReadOnlyList<int>() { 7 }
    };
    
    StartCoroutine(Batcher.ProcessPreBatched(
        batches,
        n => Debug.Log($"Processing: {n}"),
        batchDelay: 0.5f,
        onBatchStart: batch => Debug.Log($"Batch starting: {string.Join(", ", batch)}"),
        onBatchFinished: batch => Debug.Log($"Batch finished: {string.Join(", ", batch)}"),
        onFinished: all => Debug.Log("All items processed.")
    ));
    Exceptions
    Type Condition
    System.ArgumentNullException

    Thrown if actionOnItem is null.

    In This Article
    Back to top UniUtils Documentation