Introducing Plank: Immutable model generation for iOS

Pinterest Engineering
9 min readintermediate
--
View Original

Overview

The article introduces Plank, an immutable model generator for iOS, developed by Pinterest to enhance app performance and safety. It discusses the motivation behind adopting an immutable model layer, the goals for the generated code, and the capabilities of Plank in generating Objective-C models from JSON schemas.

What You'll Learn

1

How to generate immutable Objective-C models from JSON schemas using Plank

2

Why immutability enhances thread safety in iOS applications

3

When to use builder classes for model mutations in iOS

4

How to implement NSSecureCoding for model serialization

Prerequisites & Requirements

  • Understanding of JSON and Objective-C
  • Familiarity with command-line tools and Swift(optional)

Key Questions Answered

What are the benefits of using immutable models in iOS?
Immutable models are inherently thread-safe, which reduces the risk of bugs and crashes in concurrent environments. This makes the code easier to reason about, as the state of the models cannot change unexpectedly once created, leading to more predictable behavior in applications.
How does Plank generate models from JSON schemas?
Plank takes a schema file in JSON format as input and translates it into an intermediate representation before generating the corresponding Objective-C model code. This process allows for type safety and consistency across the model layer, reducing common errors associated with hand-written models.
What is the role of builder classes in Plank-generated models?
Builder classes in Plank allow for the creation of immutable objects by providing read-write properties and a build method. This pattern enables developers to set values before creating an immutable instance, ensuring that the models are populated correctly while maintaining immutability.
How does Plank handle JSON parsing for models?
Plank generates an initializer method that processes NSDictionary objects conforming to the defined schema, ensuring that properties are correctly assigned and null values are handled appropriately. This prevents issues that could arise from incorrect data types or null references.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Programming Language
Swift
Used to develop Plank, leveraging its strong type-safety features for generating immutable models.
Programming Language
Objective-c
The target language for the models generated by Plank.
Data Format
JSON
Used for defining schemas that Plank translates into model code.
Ios Framework
Nssecurecoding
Implemented in generated models for secure serialization.

Key Actionable Insights

1
Implementing an immutable model layer can significantly reduce bugs related to concurrency in your iOS applications.
By ensuring that model objects cannot be altered after creation, you can prevent unexpected behavior and crashes, leading to a more stable application.
2
Utilizing Plank to generate models can save development time and enforce consistency across your codebase.
Automating model generation reduces the manual effort required to maintain model integrity and allows developers to focus on higher-level application logic.
3
Adopting builder patterns for model mutations can simplify the process of creating complex objects.
This approach allows for a clear separation of object creation and state management, making your code cleaner and easier to maintain.

Common Pitfalls

1
Hand-written models can lead to inconsistencies and bugs due to manual errors in serialization and null checks.
These issues often arise from the complexity of maintaining multiple model types and ensuring they conform to expected behaviors, which can be mitigated by using automated tools like Plank.

Related Concepts

Immutable Models
Builder Pattern
JSON Schema
Nssecurecoding