Skip to main content

Definition

A BlocContext is an interface (currently) matching Essenty's LifecycleOwner interface:

public interface BlocContext : LifecycleOwner

If you read the chapter Lifecycle, you'll have realized how crucial the lifecycle is for the bloc. The lifecycle controls all three CoroutineScopes and thus the coroutines / jobs. The BlocContext is the object passed into a bloc's constructor holding that very lifecycle object.

Future releases of the framework might add more functionality to the BlocContext.

Creation

Every bloc takes a BlocContext in its constructor. There are extensions on both Android and iOS to create one.

Creating a BlocContext and a Bloc on Android can be as easy as:

private val bloc by getOrCreate { bloc(it) }

// more verbose:
private val bloc by getOrCreate { context -> bloc(context) }

Note that this bloc will be retained across configuration changes (Android).