Table of Contents

StorageOperation<TResult>

class in Coherence.Cloud
inherits from CloudOperation<TResult, StorageError>

Represents an asynchronous operation attempting to retrieve items from CloudStorage.

Type Parameters

TResult

Type of object returned if the operation succeeds.

Examples
// Copyright (c) coherence ApS.
// See the license file in the package root for more information.

using System.Collections;
using Coherence.Cloud;
using Coherence.Toolkit;
using UnityEngine;

class StorageOperationResultExample : MonoBehaviour
{
    public CoherenceBridge bridge = null!;
    public StorageObjectId storageObjectId = new("Object", 1);
    public ContinuationMethod continuationMethod = ContinuationMethod.Await;

    void Start()
    {
        var cloudStorage = bridge.CloudService.GameServices.CloudStorage;
        var loadOperation = cloudStorage.LoadObjectAsync(storageObjectId);

        switch (continuationMethod)
        {
            case ContinuationMethod.Await:
                AsyncMethod(loadOperation);
                return;
            case ContinuationMethod.Yield:
                StartCoroutine(Coroutine(loadOperation));
                return;
            case ContinuationMethod.ContinueWith:
                ContinueWith(loadOperation);
                return;
            case ContinuationMethod.OnSuccessAndFail:
                OnSuccessAndFail(loadOperation);
                return;
        }
    }

    IEnumerator Coroutine(StorageOperation<StorageObject> loadOperation)
    {
        // Make the coroutine wait until the operation has completed.
        yield return loadOperation;

        LogResult(loadOperation);
    }

    async void AsyncMethod(StorageOperation<StorageObject> loadOperation)
    {
        // Make the async method wait until the operation has completed.
        await loadOperation;

        LogResult(loadOperation);
    }

    void ContinueWith(StorageOperation<StorageObject> loadOperation)
    {
        // Execute LogResult after the operation has completed.
        loadOperation.ContinueWith(LogResult);
    }

    void OnSuccessAndFail(StorageOperation<StorageObject> loadOperation)
    {
        loadOperation
            // Execute an anonymous function with the result if the operation completes successfully.
            .OnSuccess(storageObject =>
            {
                foreach (var item in storageObject)
                {
                    Debug.Log($"Storage Object: '{storageObjectId}'. Item: '{item}");
                }
            })
            // Execute another anonymous function with the error if the operation fails.
            .OnFail(error => Debug.LogError(error));
    }

    void LogResult(StorageOperation<StorageObject> completedLoadOperation)
    {
        if (completedLoadOperation.HasFailed)
        {
            Debug.LogError(completedLoadOperation.Error);
            return;
        }

        // foreach can be used with StorageOperation<StorageObject>
        // to access each storage item that was loaded.
        foreach (var item in completedLoadOperation)
        {
            Debug.Log($"Storage Object: '{storageObjectId}'. Item: '{item}");
        }
    }

    public enum ContinuationMethod
    {
        Await,
        Yield,
        ContinueWith,
        OnSuccessAndFail
    }
}
Remarks

An async method can await the StorageOperation<TResult> to wait until the operation has completed.

Similarly, a Coroutine can yield the StorageOperation<TResult> to wait for it to complete.

ContinueWith(Action, TaskContinuationOptions) can also be used to perform an action after the operation has completed.

OnSuccess(Action<TResult>) and OnFail(Action<StorageError>) can be used to perform different actions based on whether the operation was successful or not.

If is a collection, you can enumerate the StorageOperation<TResult> using foreach to access each element in the collection.

If StorageOperation<TResult>.HasFailed is true, then the operation has failed. If this is the case, then Error will be non-null and contain additional information about the error.

If a StorageOperation<TResult> fails, and the error is not handled in any way (StorageOperation<TResult>.HasFailed is not checked, StorageOperation<TResult>.Error is not accessed, OnFail(Action<StorageError>) is not used, etc.), then the error will automatically be logged to the Console at some point (whenever the garbage collector releases the StorageError from memory).

Methods
ContinueWith(Action, TaskContinuationOptions)

Specify an action to perform after the operation has completed (IsCompleted becomes true.

ContinueWith(Action<StorageOperation<TResult>>, TaskContinuationOptions)

Specify an action to perform after the operation has completed (StorageOperation<TResult>.IsCompleted becomes true.

GetAwaiter()

Gets an object that can be used to await for this operation to complete. Awaiting the result of this method never causes an exception to be thrown. To handle errors you can either:

  1. Check if HasFailed is true and then examine the Error property for more details about what went wrong.
  2. Use the OnFail(Action<TError>) method to specify an action to perform if the operation fails.
OnFail(Action<StorageError>)

Specify an action to perform if the operation fails (HasFailed becomes true.

OnSuccess(Action<TResult>)

Specify an action to perform if the operation completes successfully (StorageOperation<TResult>.IsCompletedSuccessfully becomes true.

ResultToString(TResult)
Operators
implicit operator StorageOperation<TResult>(Exception)
implicit operator StorageOperation<TResult>(Task<TResult>)