Medium Thomas Ricouard Apr 11, 2024

How to do pagination in SwiftUI

Article Summary

Medium's iOS team rebuilt their search feature in SwiftUI and needed infinite scroll. Here's the elegant pagination pattern they landed on after years of iteration.

Thomas Ricouard from Medium Engineering shares the production-ready pagination technique they use across the Medium iOS app. It's a generic, reusable approach that handles loading states, error cases, and prevents duplicate requests.

Key Takeaways

Critical Insight

A battle-tested SwiftUI pagination pattern that separates state management from content rendering and keeps working even when users navigate away.

The article reveals a subtle but critical difference between .onAppear and .task that changes how pagination behaves when users switch contexts.

About This Article

Problem

Thomas Ricouard's team needed to add infinite scroll pagination to Medium's iOS search interface across multiple tabs. The challenge was preventing duplicate data fetches and keeping loads smooth when users switched between tabs.

Solution

They created a generic SearchResultListView container using List's lazy loading. A progress indicator with .onAppear triggers data fetches, while state transitions to loadingNextPage stop fetch tasks from retriggering during scroll.

Impact

Since SwiftUI's launch, this pattern has stayed production-ready and works across different features in Medium's iOS app. Teams no longer need separate pagination implementations for each feature or tab.