Documentation Index
Fetch the complete documentation index at: https://docs-unity.molca.id/llms.txt
Use this file to discover all available pages before exploring further.
File: Assets/_Molca/_Core/Networking/Data/DataManager.cs
When to use
Use DataManager instead of one-off HttpClient calls when you need:
- Persistent data providers that continuously feed data (HTTP polling, SSE, WebSocket patterns).
- Centralized caching with configurable duration and max size per provider.
- Model subscriptions — get notified when new data arrives for a specific
DataModel, with automatic batching to reduce callback frequency.
- Multiple data sources coordinated through a single gateway with provider registration/deregistration.
For simple one-shot API calls, prefer HttpClient directly.
API
Provider management
| Method | Signature | Description |
|---|
RegisterDataProvider | void RegisterDataProvider(DataProvider provider) | Register a provider and initialize its cache |
UnregisterDataProvider | void UnregisterDataProvider(DataProvider provider) | Remove a provider and its cache |
GetProvider | DataProvider GetProvider(string providerId) | Get a registered provider by ID |
IsProviderActive | bool IsProviderActive(string providerId) | Check if a provider is registered |
GetProviderIds | List<string> GetProviderIds() | List all registered provider IDs |
Data subscription and access
| Method | Signature | Description |
|---|
SubscribeToDataModel | void SubscribeToDataModel(DataModel model, Action<ImmutableData[]> callback) | Get batched notifications when data arrives |
UnsubscribeFromDataModel | void UnsubscribeFromDataModel(DataModel model, Action<ImmutableData[]> callback) | Stop receiving notifications |
GetAllData | IReadOnlyList<ImmutableData> GetAllData(string providerId) | Get cached data for a provider |
FetchData | bool FetchData(string providerId) | Manually trigger a data fetch |
RefreshAllData | void RefreshAllData() | Refresh all active providers |
IsDataStale | bool IsDataStale(string providerId) | Check if cached data exceeds duration |
Events: OnDataUpdated, OnDataProviderRegistered, OnDataProviderUnregistered (static event Action).
Code
Subscribe to model updates (recommended for live data):
using Molca;
using Molca.Networking.Data;
using UnityEngine;
public class LiveModelBinder : MonoBehaviour
{
[SerializeField] private DataModel targetModel;
private async void Start()
{
await RuntimeManager.WaitForInitialization();
DataManager.Instance?.SubscribeToDataModel(targetModel, OnDataReceived);
}
private void OnDestroy()
{
DataManager.Instance?.UnsubscribeFromDataModel(targetModel, OnDataReceived);
}
private void OnDataReceived(ImmutableData[] batch)
{
Debug.Log($"Received {batch.Length} items for model {targetModel.ModelName}");
foreach (var data in batch)
{
// Process each data item
}
}
}
Event-based (for generic data change notifications):
private void OnEnable() => DataManager.OnDataUpdated += OnDataUpdated;
private void OnDisable() => DataManager.OnDataUpdated -= OnDataUpdated;
private void OnDataUpdated(string providerId, ImmutableData data)
{
Debug.Log($"{providerId} updated");
}
Resolve the subsystem: DataManager.Instance (RuntimeManager.GetSubsystem<DataManager>()).
API Reference
Data persistence operations
| Method | Signature | Description |
|---|
Save | void Save(string key, object data) | Save data to persistent storage with the specified key |
Save<T> | void Save<T>(string key, T data) | Save typed data to persistent storage with the specified key |
Load | object Load(string key) | Load data from persistent storage by key; returns null if not found |
Load<T> | T Load<T>(string key) | Load typed data from persistent storage by key; returns default(T) if not found |
Delete | void Delete(string key) | Remove data from persistent storage by key |
Exists | bool Exists(string key) | Check if data exists in persistent storage for the specified key |
Troubleshooting
- Subscribed but no callbacks firing: verify the
DataModel.ModelId matches between the provider’s mapping and your subscription. Enable DataConfig.LogDataOperations to see subscription/flush logs.
- Data arrives but view is stale:
DataManager batches data into pools flushed every ~100ms. Call FlushAllDataPools() if you need immediate processing. Also confirm you’re subscribing to the correct DataModel instance.
- Provider not found:
RegisterDataProvider must be called before FetchData. Check that the provider’s ProviderId matches and the mapping / DataModel is assigned.
- Cache grows unbounded: configure
DefaultMaxCacheSize and DefaultCacheDuration on the DataConfig module in Global Settings.
Unity Editor