I’m live streaming my programming from start to finish on an iOS app to help folks explore and translate Scottish Gaelic phrases.
In the last post reviewing live streams 29-31, we implemented child view controllers to switch out the view on a portion of our screen in response to user actions on another portion of our screen. This time, we’ll refactor some of our (currently disparate) services to use some common architecture.
Fetching data from a URL in an iOS app involves a lot of common steps: create an asynchronous task, associate it with a promise, define a completion handler, assess the validity of the JSON that comes back, et cetera.
So far, we have been duplicating this logic in each service we extract, with one key difference: the structure of the response value differs from call to call. In one case we want back a collection of blog posts. In another case we want a list of categories. In yet another, a list of clans. I tend to avoid generalization until I see three instances of common logic (more on this refactoring strategy in this talk right here), and at this point we have reached the golden number for making the change. So let’s do that:
Then it’s time to use our common construct for each of the unique data structures we require. In this video, you’ll notice that some of our “services” still return hardcoded data rather than calling their superclass’s method for fetching from a URL. That’s because we haven’t built these endpoints yet. Nevertheless, because we have encapsulated those details within the
Service interface, we can press on with the client work and implement that later:
This refactoring is exciting to me because we’re about to add some number of new services for serving up some of the detail screens that these collection pages on the browse screen will no doubt drill into. I relish the thought of not having to do a bunch of work on the backend :). Stay tuned for that!
If you liked this piece, you might also like:
The teaching series—I teach an online class about software engineering. Then I write about my teaching techniques.
The Raft distributed consensus algorithm series—Follow along step by step as I implement Raft in Python!
The techtivism series—About evaluating our production and consumption of tech products from an ethical perspective.