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
using System.Collections;
using Coherence.Cloud;
using UnityEngine;

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

    async void Start()
    {
        PlayerAccount playerAccount = await PlayerAccount.GetMainAsync();
        var cloudStorage = playerAccount.Services.GameServices.CloudStorage;
        var loadOperation = cloudStorage.LoadObjectAsync<string>(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<string> loadOperation)
    {
        // Make the coroutine wait until the operation has completed.
        yield return loadOperation;

        LogResult(loadOperation);
    }

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

        LogResult(loadOperation);
    }

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

    void OnSuccessAndFail(StorageOperation<string> loadOperation)
    {
        loadOperation
            // Execute an anonymous function with the result if the operation completes successfully.
            .OnSuccess(text => Debug.Log($"Loaded text: '{text}'."))
            // Execute another anonymous function with the error if the operation fails.
            .OnFail(error => Debug.LogError(error));
    }

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

        Debug.Log($"Loaded text: '{completedLoadOperation.Result}'.");
    }

    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.

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>)