Skip to content

1. Context and initialization

Sergey Ivonchik edited this page Nov 4, 2021 · 8 revisions

What is Context

Context serves as main repository of all dependencies that required for library to function. It is immutable object which you must initialize before making any requests. Basically it configures logger, caches and mappers for requests and responses.

Simple default context initialization looks like this:

Context.InitializeDefault(1, () => { /* Ready to go callback */ });

You pass version number and onContextReady to this function. When onContextReady fires, you are ready to go with your requests.

But sometimes you need more control over cache sizes, have custom loggers and type-mappers. So, you can use other two arguments of InitializeDefault to configure this stuff.

Complete function signature looks like this:

InitializeDefault(
    int version, 
    Action onContextReady,
    Action<Builder> onBeforeInstantiate = null, 
    int inflateMultiplier = 1)

How to configure Context

You can configure and change all aspects of Context by using onBeforeInstantiate callback. It receives Builder of the context and exposes all the aspects of configuration.

Context.InitializeDefault(Version, () => {
  /* Ready to go callback */
}, builder => {
  builder.WithLogger(new MyLogger());
  builder.WithMapper(typeof(Json<>), typeof(NewtonsoftJsonMapper<,>));
  builder.WithMapper(typeof(Json), typeof(NewtonsoftJsonMapper<,>));
  builder.WithMemoryCache(memoryCacheSize);
  builder.WithDiskCache(diskCacheSize);
  builder.WithNativeCache(nativeCacheSize);  
});

Versioning and caching

Versioning

Parameter version controls caching. If you change version you will "fresh-up" your caches. For example: You released your app and set version = 1. User downloaded your AssetBundles and cached it offline. Then, you release update of your app and store new, updated asset bundles on the same url. You need to force app to re-download and re-cache new asset bundles so your release new app with version = 2 and user cache refreshes.

Cache sizes

Last parameter inflateMultiplier controls cache sizes, or to be more precise it simply multiplies default cache sizes so you can simply control size of all caches with single number.

Default cache sizes are very generic, for example default disk cache size is 64Mb and native caches size is 128Mb. This is for inflateMultiplier = 1. If you set inflateMultiplier = 2 you configure disk cache size to be 128Mb and native caches size to be 256Mb, and so on.

Clone this wiki locally