Assets/_Molca/_Core/Networking/Data/DataManager.cs
When to use
UseDataManager 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.
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 |
OnDataUpdated, OnDataProviderRegistered, OnDataProviderUnregistered (static event Action).
Code
Subscribe to model updates (recommended for live data):DataManager.Instance (RuntimeManager.GetSubsystem<DataManager>()).
Troubleshooting
- Subscribed but no callbacks firing: verify the
DataModel.ModelIdmatches between the provider’s mapping and your subscription. EnableDataConfig.LogDataOperationsto see subscription/flush logs. - Data arrives but view is stale:
DataManagerbatches data into pools flushed every ~100ms. CallFlushAllDataPools()if you need immediate processing. Also confirm you’re subscribing to the correctDataModelinstance. - Provider not found:
RegisterDataProvidermust be called beforeFetchData. Check that the provider’sProviderIdmatches and the mapping /DataModelis assigned. - Cache grows unbounded: configure
DefaultMaxCacheSizeandDefaultCacheDurationon theDataConfigmodule in Global Settings.
Related
- HttpClient — underlying transport for HTTP-based providers
- Global Settings —
DataConfigmodule for cache and logging settings - EventDispatcher — alternative pub/sub for simple notifications
Unity Editor
