2 min read
Data Layer
Last Updated - Platform 22.0 - SDK 17.0The data layer contains data models matching their backend API request / response counterparts or for storage, services, stores and data mappers.
The data and domain layers are usually contained within Client
(component) frameworks to be consumed by feature frameworks.
Data Models
Data models are strongly typed models for encoding to / decoding from raw data for an API or local storage. They retrieved from services or stores and mapped from / to domain models via the associated data mapper.
All SDK data models that are associated with the Poq API are Hashable
structs which conform to CodableCustomDataProvider
so that custom data can be sent to / from the backend as any JSON object. You must map your custom data in a data mapper.
The SDK is purposely complex, if you have a completely custom feature and API you do not need custom data.
Data Mappers
Data mappers map data models to domain models and vice versa.
All SDK data mappers are created as protocol interface / class implementation pairs but it's easiest to subclass the Poq
prefixed implementation.
To map customData
from SDK data models you should create your custom data model and map it using the decode
function.
Services
Services are used to retrieve or send data models to / from a backend API. You should interact with the repository using service middleware rather than using services directly.
All SDK services are created as protocol interface / class implementation pairs but it's easiest to subclass the Poq
prefixed implementation.
Networking within the SDK is in need of modernisation. For now, every SDK service adopts the GenericNetworkTaskDelegatable
and includes a nest NetworkTaskType
enum that is used to convert delegate-based request handling into completion closures.
Stores
Stores are used by some features to retrieve or store data on the device.
The SDK provides a few conveniences for this; CoreDataStore
, CoreDataObjectStore
and CoreDataObjectMapping
.
The CoreDataStore
is a convenience object for interacting with your local CoreData database.
Use it to create CoreDataObjectStore
instances for specific objects to make it much easier to work with CoreData.
Instances of CoreDataObjectStore
can be created using the CoreDataStore/store(for:limit:)
function which takes a CoreDataObjectMapping
that defines how to map an NSManagedObject
to / from a data model. When you want to store a data model in core data you should create a CoreDataObjectMapping
and either generate or create an NSManagedObject
for that model.
The CoreDataObjectMapping
is a simple interface for mapping objects without relationships. However, it is possible to use it to map complex objects with relationships using delegation via closures to map those related objects.