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 AsyncLoadable.

    See more
  • The content of this loadable. Only available if isContentsAvailable is true.

  • 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 the content or throw the error.

  • Fetch the content for this loadable, if it needs sync. Similar to syncIfNeeded().

  • Map a AsyncLoadable<C> into AsyncLoadable<T> by supplying a closure that maps C into T. This is helpful if you want to quickly map a loadable from a “thin” to a “fat” model without creating unnecessary MMMLoadableProxys. E.g. AsyncLoadable<API.User> into AsyncLoadable<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> into AsyncLoadable<T> by supplying a closure that maps C into AsyncLoadable<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 load LoadableB using a value in it’s contents, LoadableB will be exposed to the users, since that only contains valuable info for them. If LoadableA fails, we don’t have to try to load LoadableB.

    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 AsyncLoadables together, from AsyncLoadable<C> and AsyncLoadable<T> to a AsyncLoadable<(C, T)>. This could come in useful when you want to grab data from C to construct your loadable T without losing C.

    This behaves the same as flatMap(_:).

    Example

    func fetchLoadableB() -> AsyncLoadable<(AValue, BValue)> {
        loadableA().joined { aVal in
            return LoadableB(identifier: aVal.identifier)
        }
    }