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
isContentsAvailable
istrue
. -
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 thecontent
or 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 mapsC
intoT
. This is helpful if you want to quickly map a loadable from a “thin” to a “fat” model without creating unnecessaryMMMLoadableProxy
s. 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/error
and 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
sync
manually to do that. -
FlatMap a
AsyncLoadable<C>
intoAsyncLoadable<T>
by supplying a closure that mapsC
intoAsyncLoadable<T>
. This is helpful if you want to chain loadables without having to observe each one.For example say you have
LoadableA
that upon success will loadLoadableB
using a value in it’s contents,LoadableB
will be exposed to the users, since that only contains valuable info for them. IfLoadableA
fails, we don’t have to try to loadLoadableB
.If there is an error thrown in the callback, we use that as the new
AsyncLoadable/error
and 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
sync
manually to do that.Example
func fetchLoadableB() -> AsyncLoadable<BValue> { loadableA().flatMap { aVal in return LoadableB(identifier: aVal.identifier) } }
-
Join two
AsyncLoadable
s together, fromAsyncLoadable<C>
andAsyncLoadable<T>
to aAsyncLoadable<(C, T)>
. This could come in useful when you want to grab data fromC
to construct your loadableT
without losingC
.This behaves the same as
flatMap(_:)
.Example
func fetchLoadableB() -> AsyncLoadable<(AValue, BValue)> { loadableA().joined { aVal in return LoadableB(identifier: aVal.identifier) } }