Becoming A Developer: Week 15 (A New Networking Stack, Table Views, Operations, and Balance)

Mitch Little
5 min readMay 16, 2018

--

First, my new favourite quote.

Opportunity is missed by most people because it is dressed in overalls and looks like work

— Thomas Edision

Aka go out and get it.

Again, another brief update as I focus on my last uni exam. Consistency is paramount however.

Last week, I spent a lot of time getting familiar with a new networking stack to use in my future projects. The networking stack is the logic that sends requested to REST API’s, parses the data sent back into models, and throws any occurring networking errors that can then be handled gracefully.

The outline is based roughly on the implementation described in the article above by James Rochabrun, and inspired by Pasan Premaratne’s Swift 3 version.

I have however made a few adjustments mainly in the area of endpoint, url and request generation.

This stack, as the title suggests, focusses heavily on protocols and generics combined with the Codable protocol to produce a highly reusable, type safe, and call site friendly method of retrieving data from an API. Ultimately, the final outcome is a simple enumeration that contains either a populated object, or an error which can be checked with a switch statement.

With the Codable protocol too, there is no more stringly typed unwrapping using guard statements.

The implementation I will be using from now on can be seen in my most recent project here.

Codable in Swift 4 has been the most intuitive topic I have been introduced to as my time as an iOS developer has been spent already on Swift 3 which is already extremely powerful.

Although I have used different methods of retrieving data from an API and understand what is going on at a lower level, Codable takes away the overwhelming feeling that I feel when writing networking code. It always felt too counter intuitive for Swift.

Following on from this, I created the model layer of Project 7, conforming the types to the Codable protocol. I found some of the keys in the JSON returned from the API lacking detail, so for some types I provided a list of Coding Keys that allow custom stored property names but with the corresponding API key in an enumeration.

One issue I faced was unwrapping lists of objects returned in the JSON data. Firstly, I tried creating a wrapper container from the decoder, and using that wrapper container to initialise the stored properties. However this wrapper container only applied to individual objects rather than a list of objects. Currently, I have settled on providing wrapper objects that contain an array of the lower level list of objects as a stored property. This wrapper object great provides context at the use site instead of an array of objects providing by the networking stack.

Project 7 is certainly not as featureful as project 6, but not only allowed me to focus heavily on this networking stack but also a few other topics.

I made great use of Table Views in this project. Initially, I had 5 story board scenes all embedded a Navigation Controoler. Four out of five of these view controller were all exactly the same and contained a Table View.

Aligning my design to single responsibility principles, I decoupled the table view’s Data Source from the container View Controller. By doing this, I could create new instances of the View Controller and push them onto the Navigation Stack. The loading of the Table View would check for the type of data source being used as defined in an Enumeration and load the correct data source from a Data Source Manager object.

This data source would then be updated by the networking stack and the table view reloaded.

Ultimately, I had a reusable View Controller and Table View that could be adjusted by just changing the decoupled Data Source allowing for less expense of memory and great scalabilty.

In the future I would like to experiment with a reusable table view data source source:

The third major focus was the use of Operation and OperationQueue. I used these to download images with url paths provided by the initial API request.

Downloading images can be an expensive and slow task if there are many. Using the Operation class provides extra functionality. If a Table View Cell goes off screen, there is no point in continuing the image download and so using operations allows the downloading to be cancelled, until it is back in the window.

Networking, dispatch queues, and operations always help with my knowledge of concurrecy, serialism, parallelism and other related topics which I sometimes find difficult to distinguish.

On Balance

One thing I always struggle with hugely is balance.

I find it very easy to go ‘all in’ on something which isn’t necessarily a bad thing in the short term as it allows me to be laser focussed and get a lot done. However over time it causes me to start neglecting other areas of my life which are just as important.

I have a sheet that defines the three most important things in my life and as long as I am adding to one of those I can be happy. However sometimes the balance tips in one direction.

Personal relationships and social life is something I need to push more effort into as I have sacrificed a lot of time to focus on my self over the last year which is a complete oppsite to the first 19 years of my life. This isn’t a bad thing in terms of bettering myself and my life, but in the future I do not want to find myself lonely in the midst of ‘making it’.

Linking to the quote above, I have very much realised that in order to achieve things in life you have to put effort into them and work. The same goes for other areas weather it is people, family, hobbies, or health.

This post by William Cho sums it up well.

One more exam to go. I’m looking forward to writing more detailed posts and focusing my attention on iOS.

Any feedback is more than welcome and feel free to message me!

If you find these posts in anyway helpful, you can help others too by pressing the clap button!

You can learn to code and join me on Treehouse here 😊

Social: Instagram, LinkedIn, GitHub

--

--

Mitch Little

Documenting life and progress. iOS Developer. Passionate for new experiences mainly through food, people and places.