Last updated 1 min read

Using SwiftletModel as Normalized Cache

#SwiftletModel

One of the ways to utilise SwiftletModel is to put it in front of the API client and use it as a normalized cache.

Normalization

SwiftletModel is capable of data transformation between normalized<-> denormalized forms, which is only limited by the model's shape, which is defined by entities' relationships.

Incomplete Data Handling

SwiftletModel supports an entire toolset of merging strategies for handling incomplete/missing data at the entity and entity relationship levels: incomplete models, incomplete model properties of collection type, or incomplete collections of related entities.

It's up to the developer to decide for any particular datasource or endpoint if it is a source of truth for the entire dataset or not and then update the SwiftletModel entity graph with a proper option.

The last write wins merge strategy allows for updating entity models based on timestamps. It can be combined with the patch non-nils strategy, which makes the whole thing very powerful.

BI-directional relations

Bi-directional relations can be defined declaratively. SwiftletModel would handle all the rest.

In Practice

Put it another way. SwiftletModel allows defining merge strategies on the nodes and edges level of the object graphs and merging different object graphs in a declarative way, keeping the result data consistent.

Sounds complicated?

In practice, that means that SwiftletModel allows building a normalised cache layer declaratively to solve the whole class of data inconsistency problems that would arise in the case of plain URLSession caching.

Furthermore, SwiftletModel shines best when used together with plain URLSession cache because in that case, developers can get both data consistency and persistence.