Categories
Uncategorized

Dynamically loading a hierarchical list in SwiftUI

Today’s challenge: use the standard SwiftUI list view to navigate through a folder structure. All examples I could find work based on a pre-defined nested data structure, but that won’t work for a folder / file hierarchy: the nesting can be so deep that loading performance will be not acceptable. Especially when browsing a network share.

The solution is to add an onAppear handler that is called whenever an item in the hierarchy is shown, and trigger the load of the children of that node. By using async/await for the loading, the UI stays responsive.

The following gist shows an implementation example, with some load optimisations so that a folder is only loaded once.

Note: the original description talked about a 2-level retrieval, which is actually not needed and results in poor performance on network shares. The gist is updated.