This is a sample application that demonstrates how to use the ApolloPagination
library in a SwiftUI application.
The two main components of the sample are:
- Setting up and using the
AsyncGraphQLQueryPager
withinLaunchListViewModel
to fetch and display a paginated list of launches. - Sending signals to fetch the next page within
LaunchListView
when the user scrolls to the bottom of the list.
The sample application is built around the AsyncGraphQLQueryPager
, as opposed to the GraphQLQueryPager
, because SwiftUI has a built-in mechanism for entering an asynchronous context.
AsyncGraphQLQueryPager
:
- At present, there is potential for a race condition depending on the way the initial fetch is triggered. See this comment for more information.
GraphQLQueryPager
:
- The
GraphQLQueryPager
does not have callbacks available for when the initialfetch
(orrefetch
) complete, but should in the next release.
Usage with SwiftUI
:
- Secondary Triggers
- The
task
which is used to fetch next pages must have a secondary trigger, as the standardtask
is not sufficient for triggering the next page fetch. This is because thetask
is only triggered when the view will appear, but there are situations where a fetch is disallowed. For example, if the user triggers a pull-to-refresh action, and very quickly scrolls to the bottom of the page prior to the completion of the refresh, the pager will not fetch the next page, as it will lead to inconsistent state. Thus, thetask
must be triggerd by a secondary action, after the completion of therefresh
action. This is demonstrated in theLaunchListView
by using theloadState
as a secondary trigger. - The
task
modifier will cancel any operation when the view will disappear. This is not ideal for a pager, as the user may want to continue fetching pages even when the view is not visible. This can be worked around by using a customtask
modifier, or by using a combination ofonAppear
andonChange
. - The need for a secondary trigger may cause the
loadNextPage
to be called multiple times. This is not an issue, as the pager will only fetch the next page if it is not already fetching a page – but the pager will throw aloadInProgress
error on invocation of theloadNextPage
method if it is already fetching a page.PaginationError
s are not fatal errors, and are largely to inform the developer of a misuse. They can be ignored, or handled as necessary.
- The