Vault Save System

Async API

Offloads disk I/O to a background thread so the main thread never stalls. Serialization still happens on the calling thread (Unity-API safe).

API

static Task SaveAsync<T>(string key, T value) static Task<T> LoadAsync<T>(string key, T defaultValue = default) static Task SaveAsync(string key, ScriptableObject so) static Task FlushAsync()

SaveAsync does two things: it serializes your data on the calling thread (the value is captured right then, and it stays Unity-API safe), then hands the actual file write to a background thread and returns a Task. Because that write runs in the background, the data is not necessarily on disk yet when SaveAsync returns - even the returned Task completing only means that write finished.

FlushAsync is a barrier. The Task it returns completes only once every queued background write has finished hitting disk (across all drawers). Await it wherever you need a durability guarantee - above all before the app quits or is backgrounded - so a still-pending save is never lost. Awaiting an individual SaveAsync guarantees that one write; FlushAsync waits for all of them at once without you tracking each Task.

Serialization is always synchronous, so the value you pass is snapshotted at the call site - mutating the object afterwards won't corrupt the write already in flight.

Usage

using Warpdev.Vault;
using UnityEngine;

public class SaveButton : MonoBehaviour
{
    // Async save - await so you know when the data is on disk
    public async void Save()
    {
        await Vault.SaveAsync("player.health", 100);
        Debug.Log("Saved.");
    }

    // Non-async handler - hook this up to a UI Button's OnClick
    public void OnSaveButtonPressed()
    {
        Save();   // fires the save; the disk write runs on a background thread
    }
}

Save on Quit / Pause

Flush before the app closes so nothing is lost:

async void OnApplicationQuit()
{
    Vault.SaveAsync("player.health", currentHealth);
    await Vault.FlushAsync();   // wait for the background write to finish before we exit
}

void OnApplicationPause(bool paused)
{
    if (paused) _ = FlushOnPause();   // Android/iOS: save when backgrounded
}

async Task FlushOnPause()
{
    Vault.SaveAsync("player.pos", transform.position);
    await Vault.FlushAsync();
}

In Unity's Edit Mode test runner, async Task tests aren't supported. Use .GetAwaiter().GetResult() to block synchronously in tests.