Share
Embed
Managing "Network States" with protocol extensions
Swift India
February 09, 2019
Transcript
26 slides
-
Managing “Network States” with protocol extensions Lubaba Hasnain Swift Bangalore
Chapter 12, Saturday Feb 9th, 2019 -
How many times in a networked application have you met
with states like these..? -
Content Data is loaded successfully from the server. It is
available and presented to the user. -
Empty Network call is successful. But there is no data
to load the view. -
Loading Network call is still in progress. So, the user
has to wait on that screen till the data is completely loaded. -
Error Error encountered while loading data over network. No Internet
connection/internal server error/user unauthorised, etc -
In my last projects, I used them a lot. But
you know, state management was not proper in my code. Problems with the current state management in the applications we build. -
Massive View Controllers A single view controller is tasked with
managing the appearance of many other views, controls and other user interface elements based on state. -
1. A view controller is handling many responsibilities 2. What
if more states are added? -
- Class will become bloated - Number of lines of
code will increase…. eventually leading to massive view controller issue - Some people prefer to have all their state management code in BaseViewController (A controller which all the other view controller implements) -
- Multiple inheritance not allowed in swift - Code is
not re-usable. Leads to code duplication & bad design. - Are you following DRY pattern? - Will code refactoring be easier following this kind of architecture? -
At WWDC 2015, Apple introduced protocol-oriented programming. With it came
the most powerful feature: Protocol Extensions. Solution -
1. Achieve multiple inheritance as Swift allows multiple inheritance of
protocols 2. Re-usable code 3. Protocols can be implemented by a class, struct or enum. Protocols can have default implementations for requirements specified in a protocol extension allowing `mixin` or `trait` like patterns. -
1. StateType Enum At first we will create an enum
called StateType where we will define network states like error, empty, content, loading and a default state called none. -
2. State Manager class StateManager is a class that will
manage the addition/removal of views. It has access to the current view state being displayed on the screen. 1. viewstore variable -> to store the views on the basis of the state type 2. getView method -> Returns the view for a given state 3. addView method -> Associates/adds a view for the given state 4. removeView method -> Removes a view for the given state 5. removeAllViews method -> Remove all views and empty the viewstore property -
3. ViewStateProtocol = Magic ViewStateProtocol is the protocol that should
be implemented by any View Controller class to add/remove view for a given state. It has some abstract methods and properties. The extension of ViewStateProtocol has the default implementation of those abstract methods and properties. -
Main Advantages of this kind of architecture Code - reusability
Any view controller class can implement this protocol to add/remove views without writing any extra line of code. Multiple Inheritance Protocol extension allows us to retain one of the best features of subclassing(inheritance). Using protocols allows us to inherit from multiple parents simultaneously Not Limited to classes Protocols can be adopted by classes, enums or structs whereas base classes and inheritance are available for classes only. Conditional Extensions By using where keyword, we only extend protocol for types that inherit from UIViewController, we are able to use UIViewController specific methods. -
References 1. https://github.com/aschuch/StatefulViewController 2. https://medium.com/@lubabahasnain93/managing-network-states-with-pr otocol-extensions-cab07ed9048d 3. http://ioscoachfrank.com/2018/01/07/stateful-content-controller.html 4. http://www.vadimbulavin.com/multiple-inheritance-swift/
5. https://medium.com/@abhimuralidharan/all-about-protocols-in-swift-11a7 2d6ea354 6. https://www.swiftbysundell.com/posts/custom-container-view-controllers -in-swift -
Conclusion - Managing network states seems trivial but there’s lot
of state management involved - The biggest challenge comes in re-using the code but most tutorials tend to ignore them - Proper architecture to manage states in any networked application. - Protocol extensions is a powerful feature that needs to be explored more.