Skip to content

IDataService

IDataService is responsible for caching a variety of data objects inside IdleKit. The main use case for the service is caching static data associated with IEntities. Data Service uses specialized IData objects and Data Provider wrappers when operating with data.

IDataProvider wrappers and their types

IDataProvider is used to wrap one or more IData objects to process them before the IDataService.AddData method retrieves them. This is especially useful when the data needs to be flattened or otherwise modified before being passed into the IDataService.

There are 2 variations of Data Providers: ISingleDataProvider and IMultipleDataProvider. The former returns an IData object and the latter returns an array of IData objects. One example of an implemented Data Provider wrapper is in BaseMultipleDataProvider, which implements the IMultipleDataProvider interface, and flattens the IData arrays contained inside of it to pass them into the IDataService as a single array of IData.

Methods

IDataService operates on the basis of 3 main methods, all of which have a variety of overloads.

Adding and Removing Data

IdleKit.Core.IDataService.AddData

IDataService uses AddData method to cache data for later retrieval. The method is provided with 3 different overloads to allow data addition in bulk as well as through passing in an Data Provider into the method. Static data inside IdleKit is usually added to the service during one of the startup ILoadphases?" of IdleKit.

Starting with the version 2.0 IdleKit uses Beamable as its chosen backend service, which means that the static data comes from the IMultipleDataProvider designed to flatten retrieved data from Beamable's servers - BaseMultipleDataProvider. In IdleKit's default implementation of the service - Data Service - every time you add static data through the AddData method Data Service dispatches a StaticDataAddedAction using the ActionService to notify the rest of the application that the static data is ready to be accessed through the service.

IdleKit.Core.IDataService.RemoveData

IDataService uses the RemoveData method to remove the cached data from the service, so it can't be retrieved. Just like the AddData method, there are several overloads provided for this method in order to remove IDatas one by one, in bulk, or through an IDataProvider. Data inside IdleKit is usually removed from the service when switching between contents or when the game needs to reset. Examples of these uses can be found inside the RemoveDataProvidersPhase.

IDataService also provides a RemoveAllData method, which allows you to remove all of the service's cached data or alternatively remove all data of a certain type.

Retrieving Data

IdleKit.Core.IDataService.GetData

This method allows Data Service to perform its main function of providing static data to IEntities and other classes inside IdleKit. The most common use case for this method can be found inside any IEntity's Inject method, as the IdleKit Entities use IDataService's GetData method to retrieve their static data for further use. The most commonly used way to retrieve static data from IDataService in IdleKit is through the Entitiy Id, as the Id is already contained inside each Entity.

IDataService also provides a GetAllData method, which allows you to retrieve all IData of a certain type which meets the specified predicate.

Flow Diagram

The following diagram demonstrates the flow of static data from Beamable's servers into IDataService's default implementation and then into the Entities which request this data.

dataservice_data_flow