GenericRepository

class GenericRepository<E : Any>(networkDataSource: NetworkDataSource, localDataSource: LocalDataSource<E>, responseType: TypeInfo, currentTimeMillis: () -> Long = System::currentTimeMillis, scope: CoroutineScope? = null) : Repository<E>

The default Repository: reads/writes a LocalDataSource and falls through to the network per CachePolicy, always writing through to local storage on a successful network load. Mirrors SwiftyNetwork's GenericRepository.

Concurrent loads for the same CacheKey are coalesced: a burst of callers that all miss the cache triggers a single network request whose result they all await — the same single-flight pattern the network layer uses for token refresh.

Parameters

scope

coroutine scope hosting the coalesced loads. Pass your own to tie loads to a lifecycle you control; when omitted, an internal SupervisorJob on Dispatchers.Default is created and close cancels it. close never cancels a scope you supplied.

Prefer the reified GenericRepository(networkDataSource, localDataSource) factory below, which captures responseType for you.

Constructors

Link copied to clipboard
constructor(networkDataSource: NetworkDataSource, localDataSource: LocalDataSource<E>, responseType: TypeInfo, currentTimeMillis: () -> Long = System::currentTimeMillis, scope: CoroutineScope? = null)

Functions

Link copied to clipboard
fun close()

Cancels in-flight coalesced loads and releases the internal coroutine scope. No-op when a caller-supplied scope was provided — cancel that scope yourself.

Link copied to clipboard
open suspend override fun fetch(endpoint: NetworkEndpoint, cacheKey: CacheKey, policy: CachePolicy = CachePolicy.Default): E

Returns the entity for endpoint, honoring policy against the value cached under cacheKey.

Link copied to clipboard
open override fun stream(endpoint: NetworkEndpoint, cacheKey: CacheKey, policy: CachePolicy = CachePolicy.Default): Flow<E>

Observes the entity for endpoint/cacheKey reactively: emits the result of an initial fetch, then re-emits whenever the underlying local store reports a change for cacheKey.

Link copied to clipboard
open override fun streamOrNull(endpoint: NetworkEndpoint, cacheKey: CacheKey, policy: CachePolicy = CachePolicy.Default): Flow<E?>

Observes the entity like stream, also emitting null when its local value is removed.