Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Managing "Network States" with protocol extensions

Swift India
February 09, 2019

Managing "Network States" with protocol extensions

Swift India

February 09, 2019
Tweet

More Decks by Swift India

Other Decks in Technology

Transcript

  1. Content Data is loaded successfully from the server. It is

    available and presented to the user.
  2. Loading Network call is still in progress. So, the user

    has to wait on that screen till the data is completely loaded.
  3. Error Error encountered while loading data over network. No Internet

    connection/internal server error/user unauthorised, etc
  4. 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.
  5. 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.
  6. - 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)
  7. - 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?
  8. At WWDC 2015, Apple introduced protocol-oriented programming. With it came

    the most powerful feature: Protocol Extensions. Solution
  9. 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.
  10. 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.
  11. 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
  12. 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.
  13. 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.
  14. 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.