Table of Contents

StorageOperation

Represents an asynchronous CloudStorage operation.

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 StorageOperationExample : MonoBehaviour
{
    public CoherenceBridge bridge = null!;
    public StorageObjectId storageObjectId = new("Object", 1);
    public StorageItem storageItem = new("Item", 1);
    public ContinuationMethod continuationMethod = ContinuationMethod.Await;

    void Start()
    {
        var cloudStorage = bridge.CloudService.GameServices.CloudStorage;
        var saveOperation = cloudStorage.SaveObjectAsync(storageObjectId, storageItem);

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

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