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
AsyncGraphQLQueryPagerwithinLaunchListViewModelto fetch and display a paginated list of launches. - Sending signals to fetch the next page within
LaunchListViewwhen 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
GraphQLQueryPagerdoes not have callbacks available for when the initialfetch(orrefetch) complete, but should in the next release.
Usage with SwiftUI:
- Secondary Triggers
- The
taskwhich is used to fetch next pages must have a secondary trigger, as the standardtaskis not sufficient for triggering the next page fetch. This is because thetaskis 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, thetaskmust be triggerd by a secondary action, after the completion of therefreshaction. This is demonstrated in theLaunchListViewby using theloadStateas a secondary trigger. - The
taskmodifier 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 customtaskmodifier, or by using a combination ofonAppearandonChange. - The need for a secondary trigger may cause the
loadNextPageto 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 aloadInProgresserror on invocation of theloadNextPagemethod if it is already fetching a page.PaginationErrors are not fatal errors, and are largely to inform the developer of a misuse. They can be ignored, or handled as necessary.
- The