Immutable models and data consistency in our iOS App

Pinterest Engineering
6 min readbeginner
--
View Original

Overview

The article discusses the transition of Pinterest's iOS app to a completely immutable model layer, highlighting the motivations behind this architectural change and how it enhances data consistency and thread safety. It also explores the methods for updating models and handling API data efficiently.

What You'll Learn

1

How to implement immutable models in your iOS application

2

Why immutability improves thread safety in concurrent applications

3

How to update models using builder patterns

4

How to use NSNotificationCenter for model updates

Prerequisites & Requirements

  • Understanding of object-oriented programming concepts
  • Familiarity with iOS development and Objective-C

Key Questions Answered

What are the benefits of using immutable models in iOS apps?
Immutable models prevent unintended side effects from shared state, ensuring that once a model is created, it cannot be modified. This leads to safer code, especially in concurrent environments, as multiple threads can read the model without risk of inconsistency.
How does Pinterest update its immutable models?
Pinterest updates its immutable models by creating new model objects instead of modifying existing ones. This can be done using a dictionary for initialization or a builder pattern that allows modifications before creating a new instance.
What approach does Pinterest use for data consistency in its app?
To maintain data consistency, Pinterest switched from Key-Value Observing to a NSNotificationCenter-based system. This allows different parts of the app to be notified when a model is updated, ensuring that views reflect the latest data.
How does Pinterest handle API data loading and caching?
Pinterest's API allows for partial JSON model requests, reducing data transfer. They utilize a central model cache built on PINCache, merging new server responses with existing models to keep the cache updated with the most recent data.

Technologies & Tools

Backend
Pincache
Used for caching model data in the iOS app.

Key Actionable Insights

1
Implement immutable models to enhance the reliability of your application.
By using immutable models, you can avoid unintended side effects from shared state, which is crucial in multi-threaded environments. This leads to fewer bugs and a more predictable application behavior.
2
Utilize a notification system for model updates to ensure UI consistency.
Switching to a notification-based system allows your views to react to model changes dynamically, improving user experience and reducing the chances of displaying stale data.
3
Adopt a builder pattern for model updates to maintain immutability.
Using a builder pattern allows you to create new instances of models with modified properties while keeping the original models unchanged, which is essential for maintaining data integrity.

Common Pitfalls

1
Failing to manage state changes properly in a mutable model can lead to inconsistent UI.
When multiple parts of an application reference the same mutable model, changes made by one part can unexpectedly affect others. This can cause bugs that are difficult to trace, especially in a multi-threaded environment.

Related Concepts

Immutability In Programming
Concurrency In Ios Development
Model-view-controller (mvc) Architecture