X

Get in Touch

iOS App Architecture Best Practices

blog image
Apr
17
2018

Design patterns and architecture are very important these days in creating a reliable and successful application and there, people are stuck on a question about choosing the architecture for iPhone app development. Therefore, it is obvious to discuss the features of a good architecture and the benefits you could gain by having a good architecture for your iOS app development.

Why do we care to choose the correct architecture?

This is because someday debugging a huge class with dozens, different things will be found difficult and you will be unable to find and fix the errors in your class. It is always not possible to keep the class in mind as a whole entity, which can cause missing of some important details. If you are already in this situation it is very likely that

  • This class is the UI view Controller subclass
  • Your data is directly stored in UI View Controller
  • Your UI views do almost nothing
  • The model is a dumb data structure
  • Your Unit Test covers nothing

This can happen despite the fact that you are covering apple’s guidelines and implementing Apple’s MVC patterns.

What is a good architecture?

A few traits are always needed from a specific application’s architecture. Each object should have a clear role, which is easy to understand and easy to change. When you go and read the source code, it should be immediately seen whether it is fulfilling the single role. It should have a single data flow, which can be easily debugged in case of a crash or error. Therefore some features of a good architecture include:

  • Balanced distribution among entities
  • Measurability
  • Testability
  • Ease of use and maintainability

Distribution: This keeps a good amount of load on our brain while we try to figure out how things work. If you believe- 'the more you develop, the more your brain will adapt to understand the complexity’, then you are right. However, this scaling is not linear and reach the top very quickly. Therefore, the best way to capture the complexity is to divide the responsibilities among multiple entities following the single responsibility principle.

Testability: This a not a concern if you already felt gratitude to unit tests which failed after adding new features or due to refactoring some intricacies of the class. This means that the test saved those developers from finding issues during the runtime, which might happen while the app is on a user’s device and the fix requires a week to reach the user.

Ease of Use: The best code is the code that has never been written. Which means the less code you have, the fewer errors you have in them. This means the desire to write less code is not merely the laziness of the developer, and you should not favor a smarter solution closing your eyes to its maintenance cost.

We have many design patterns that we can apply based on requirements of our project, like:

  • MVC
  • MVP
  • MVVM

MVC(Model View Controller): 

It is a widely used pattern for creating software applications. Most of the Cocoa application and frameworks created by Apple have implemented this design pattern. 

  • This model is where your domain data resides. Things like persistence, networking codes, model objects and parsers, which manipulate the data, stay here.
  • The view is the face of your application. This is responsible for the user interface and handles the user interactions.
  • The controllers act as glue or mediators between the model layers and presentation layers. It alters the model by reacting to actions performed by the user on view and updating the view that comes from the changes in the model.

In MVC if we try, building complex apps it gets difficult, more and more codes are transferred to the controllers, making them more fragile. The controllers are so tightly fitted that if we try to change something in the view, we have to make the changes from the controller and this violates the balanced distribution among the entities.

MVP comes as a rescue now

MVP(Model View Presenter):

Cocoa’s MVC promise is fulfilled here. This fulfills testability surface and clean separation of view and model. 

  • This is same as MVC’s model, which manages reading and writing data and persisting states.
  • The view part includes both view and view controllers.
  • The presenter contains the logic that handles user interactions. The responsibility of the presenter is to communicate with the model, convert the data to a user-friendly format, and then update the view.

MVVM(Model View View Model):

 This is the latest of Model view patterns. This is an implementation of observer design pattern where any changes are represented in the view and view model.

  • Model: This represents a data model that our app consumes. This class declares the properties for managing the business data similar to the above two design patterns.
  • View: It is similar to MVP. The MVVM view includes both view and view controllers. It simply holds data and delegates everything to the model.
  • View Model: The view Model act as a link between the model and view, responsible for wrapping up the model and preparing the observable data needed by the view. 

The MVVM satisfies all the features of a good architecture. But as we have heard ”with great power comes great responsibility” Its is easy to mess up with Reactivecocoa, if something went wrong you have to spend a lot of time fixing the issue. This pattern cannot be used for simple limited screen applications making the code more complex and difficult to maintain for the developers.

Hope this article has helped you with the importance of choosing a correct architecture and design pattern for your custom iPhone app development based on the requirements and scale of your application.
 

.

Get in Touch