AsyncLoadable
AsyncLoadable is a concrete implementation of AsyncLoadableProtocol, subclass from this class to make
your own loadable, so you can avoid working with generics “down the line”.
-
Errors thrown by the
See moreAsyncLoadable. -
The content of this loadable. Only available if
isContentsAvailableistrue. -
If the content is available, defaults to
content != nil, but you can override this to supply additional conditions. -
Call this to successfully sync the loadable.
-
Fetch the content for this loadable. Similar to
sync()only we immediately return either thecontentor throw theerror. -
Fetch the content for this loadable, if it needs sync. Similar to
syncIfNeeded(). -
Map a
AsyncLoadable<C>intoAsyncLoadable<T>by supplying a closure that mapsCintoT. This is helpful if you want to quickly map a loadable from a “thin” to a “fat” model without creating unnecessaryMMMLoadableProxys. E.g.AsyncLoadable<API.User>intoAsyncLoadable<Models.User>.If the original loadable is already synced / has contents available, we map it directly.
If there is an error thrown in the callback, we use that as the new
AsyncLoadable/errorand set it to failed.Example
func fetchUser() -> AsyncLoadable<Models.User> { // apiClient.getUser() returns AsyncLoadable<API.User> apiClient.getUser().map { apiUser in return Models.User(apiModel: apiUser) } } -
Similar to
map(_:)but with the ability to supply a async callback.Please note that unlike the map function, that doesn’t take an async closure, if the content is available, we don’t map it directly, you will have to sync the loadable again. The original loadable won’t ever sync again if content is available, you will have to call
syncmanually to do that. -
FlatMap a
AsyncLoadable<C>intoAsyncLoadable<T>by supplying a closure that mapsCintoAsyncLoadable<T>. This is helpful if you want to chain loadables without having to observe each one.For example say you have
LoadableAthat upon success will loadLoadableBusing a value in it’s contents,LoadableBwill be exposed to the users, since that only contains valuable info for them. IfLoadableAfails, we don’t have to try to loadLoadableB.If there is an error thrown in the callback, we use that as the new
AsyncLoadable/errorand set it to failed.Please note that unlike the map function, that doesn’t take an async closure, if the content is available, we don’t map it directly, you will have to sync the loadable again. The original loadable won’t ever sync again if content is available, you will have to call
syncmanually to do that.Example
func fetchLoadableB() -> AsyncLoadable<BValue> { loadableA().flatMap { aVal in return LoadableB(identifier: aVal.identifier) } } -
Join two
AsyncLoadables together, fromAsyncLoadable<C>andAsyncLoadable<T>to aAsyncLoadable<(C, T)>. This could come in useful when you want to grab data fromCto construct your loadableTwithout losingC.This behaves the same as
flatMap(_:).Example
func fetchLoadableB() -> AsyncLoadable<(AValue, BValue)> { loadableA().joined { aVal in return LoadableB(identifier: aVal.identifier) } }
View on GitHub
AsyncLoadable Class Reference