Table of Contents

StorageOperation

Represents an asynchronous CloudStorage operation.

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

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

    async void Start()
    {
        PlayerAccount playerAccount = await PlayerAccount.GetMainAsync();
        var cloudStorage = playerAccount.Services.GameServices.CloudStorage;
        var saveOperation = cloudStorage.SaveObjectAsync(storageObjectId, "Hello, World!");

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

    IEnumerator Coroutine(StorageOperation saveOperation)
    {
        // Make the coroutine wait until the operation has completed.
        yield return saveOperation;

        LogResult(saveOperation);
    }

    async void AsyncMethod(StorageOperation saveOperation)
    {
        // Make the async method wait until the operation has completed.
        await saveOperation;

        LogResult(saveOperation);
    }

    void ContinueWith(StorageOperation saveOperation)
    {
        // Execute LogResult after the operation has completed.
        saveOperation.ContinueWith(LogResult);
    }

    void OnSuccessAndFail(StorageOperation saveOperation)
    {
        saveOperation
            // Execute an anonymous function if the operation completes successfully.
            .OnSuccess(() => Debug.Log($"Object '{storageObjectId}' saved in cloud storage successfully."))
            // Execute another anonymous function with the error if the operation fails.
            .OnFail(error => Debug.LogError(error));
    }

    void LogResult(StorageOperation completedSaveOperation)
    {
        if (completedSaveOperation.HasFailed)
        {
            Debug.LogError(completedSaveOperation.Error);
            return;
        }

        Debug.Log($"Object '{storageObjectId}' saved in cloud storage successfully.");
    }

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

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

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

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

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

If a StorageOperation fails, and the error is not handled in any way (StorageOperation.HasFailed is not checked, StorageOperation.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

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

GetAwaiter

Gets an object that can be used to await for this operation to complete.

OnFail

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

OnSuccess

Specify an action to perform if the operation completes successfully (IsCompletedSuccessfully becomes true.

Operators
op_Implicit