Pageable

public class Pageable<T>

A Pageable instance is a cached view on a remote collection of elements of the specified type T.

Notion of Pages The remote collection is not entirely loaded in memory but only a subset of the elements is loaded in the cache. This subset is called a page and has a configurable limit() limit and offset() offset to respectively control the maximum number of elements to fetch from the remote collection and the absolute offset of the first element to fetch. The elements loaded in the cache are accessed using elements().

Navigation The Pageable instance is also used to navigate in the remote collection and access its elements. The navigation supports random access to any position in the remote collection and is performed by calling one of the following methods: next(callback:), first(callback:), last(callback:), at(offset:callback:). Navigation implies accessing the remote collection and may fail with an I/O error.

Concurrent modifications of the remote collection The remote collection might be modified concurrently. Consequences:

  • Changes that occurred in the remote collection are not reflected in the elements in the cache accessed with elements()
  • The total size of the collection might have evolved since last access and may affect the result of next(callback:), at(offset:callback:) and last(callback:)
  • Get the total number of elements in the collection

    Declaration

    Swift

    public func size() -> Int

    Return Value

    The size of the collection.

  • Returns whether or not more elements are available in the remote collection, accessible using next(callback:).

    Declaration

    Swift

    public func hasMore() -> Bool

    Return Value

    true if more elements are available.

  • Get the absolute offset in the remote collection of the first element.

    Declaration

    Swift

    public func offset() -> Int

    Return Value

    The offset of the first element.

  • Get the maximum number of elements that will be fetched from the remote collection during next refill.

    Declaration

    Swift

    public func limit() -> Int

    Return Value

    The maximum number of elements to fetch.

  • Set the number of elements that will be retrieved from the remote collection during next refill.

    Declaration

    Swift

    public func limit(limit: Int) -> Pageable<T>

    Parameters

    limit

    The maximum number of elements of a page.

    Return Value

    Pageable instance configured with the new limit (could be the same object).

  • Returns the subset of elements that have been loaded from the remote collection.

    Declaration

    Swift

    public func elements() -> [T]

    Return Value

    The elements of current page.

  • Navigate to the first page in the remote collection.

    Throws

    ClientError.network if an error occurred while trying to access remote collection.

    Declaration

    Swift

    public func first(callback: @escaping (Pageable<T>?, ClientError?) -> Void)
            throws

    Parameters

    callback

    Callback containing the Pageable<T> If an error occurs page is set to nil and error indicates the error.

  • Navigate to the next page in the remote collection.

    Throws

    • ClientError.network if an error occurred while trying to access remote collection.
    • ClientError.state if the collection has no more elements.

    Seealso

    hasMore()

    Declaration

    Swift

    public func next(callback: @escaping (Pageable<T>?, ClientError?) -> Void)
            throws

    Parameters

    callback

    Callback containing the Pageable<T> If an error occurs page is set to nil and error indicates the error.

  • Navigate to the last page in the remote collection.

    Throws

    ClientError.network if an error occurred while trying to access remote collection.

    Declaration

    Swift

    public func last(callback: @escaping (Pageable<T>?, ClientError?) -> Void) throws

    Parameters

    callback

    Callback containing the Pageable<T> If an error occurs page is set to nil and error indicates the error.

  • Navigate to the page at the specified offset in the remote collection.

    Throws

    • ClientError.network if an error occurred while trying to access remote collection.
    • ClientError.state if the offset is out of range (offset < 0 || offset >= size()).

    Declaration

    Swift

    public func at(offset: Int, callback: @escaping (Pageable<T>?, ClientError?) -> Void)
            throws

    Parameters

    offset

    The absolute offset of the first element to fetch.

    callback

    Callback containing the Pageable<T> If an error occurs page is set to nil and error indicates the error.