In a recent post, I presented an approach for succeeding on take-home iOS-developer coding challenges. (For brevity, I henceforth refer to these particular coding challenges as “coding challenges”.) The model solution in that post used UIKit because, at the time I wrote the post, I had already completed coding challenges using that framework. But SwiftUI may be a good, or indeed the best, option.
My goal in this post is to help readers who are are considering or have been assigned a SwiftUI-based coding challenge.
This post presents factors for the UIKit-or-SwiftUI decision. This post then addresses certain challenges posed by a SwiftUI solution, including architecture, dependency injection, testing, image caching, and Identifiable
.
In the course of discussing these considerations and challenges, this post introduces a SwiftUI model solution, KatFancy, and uses that solution for illustrative purposes.
To derive maximum benefit from this post, readers should review the original post before reading this one. Most of the content of that post is relevant to all coding challenges.
URLSession
“and related classes provide an API for downloading data from … endpoints indicated by URLs.” Most iOS developers are familiar with using the URLSession
singleton, shared
, which has “reasonable default behavior”, including retrieving data from the actual endpoint represented by the URL
specified.
But using shared
in all circumstances has some drawbacks.
shared
means that development of the client-side UI of a new feature is blocked until development of the endpoint is complete.shared
necessarily involves network access, use of shared
in unit tests can cause those unit tests to be slow or to fail altogether.This post presents a solution to these three problems: using a stubbed version of URLSession
and using alternate URL
variants, both via dependency injection.
Paul Hudson described the URLSession
-stubbing technique in this excellent article. My post contains two refinements to his article, both described in the section Acknowledgement
.