Sub topic 1
How Lightning Data Service and its cache back uiRecordApi
Prerequisites
This chapter assumes you already know:
- JavaScript basics, including modules, promises, and async/await.
- The structure of a Lightning web component: an HTML template, a JavaScript class, and a metadata file.
- The
@apidecorator and therecordIdproperty that record pages set. - Salesforce objects, fields, and record IDs.
You do not need to have written a wire yet. Later chapters cover the @wire decorator and getRecord in detail.
What Lightning Data Service is
Lightning Data Service (LDS) is the client-side data layer of Lightning Experience. It loads records through the User Interface API, keeps them in a cache in the browser, and hands them to components.
The lightning/uiRecordApi module is the LWC entry point to that layer. Its wire adapters, such as getRecord, and its functions, such as updateRecord, all go through LDS. They do not call Apex, and you write no server code.
LDS also applies the user's permissions. Field-level security and sharing rules decide what the server returns, so a component can only see what the current user can see.
The shared record cache
LDS keeps one cached copy of each record, and every component on the page shares it. Suppose a record page has a header component and a details component, and both read the same account. LDS makes one server request and gives both components the same data.
Sharing the cache has three effects:
- Fewer network requests, because a record that is already cached is served from memory.
- Consistent data, because components read one copy and cannot disagree about the record's values.
- Automatic propagation, because a change made through LDS updates the cache, and every wire reading that record receives the new values.
The cache stores data per field. If one component has loaded Name and another asks for Name and Industry, LDS only needs to fetch the missing field.
How a read and a write move through LDS
The diagram below shows two components that share a record. One saves a change, and the other updates without any extra code.
Reading uses a wire adapter, and updating uses an imperative function. A wire adapter is a provider that LDS pushes data through, and it re-emits whenever the cached record changes. An imperative function runs once when you call it and returns a promise.
When the cache does not know about a change
LDS can only keep the cache current for changes it sees. If a record changes some other way, the cache is stale until it is told otherwise. Common examples:
- An Apex method updates the record on the server.
- A flow, a trigger, or another user changes the record.
In those cases the wire keeps serving the old cached values. The fix is getRecordNotifyChange, which marks cached records as stale so wires reload them. A later chapter covers it.
Summary of the model
| Piece | Role |
|---|---|
| LDS | Loads records through the User Interface API and enforces user permissions |
| Cache | Holds one copy of each record, shared by all components on the page |
| Wire adapter | Reads data and re-emits when the cached record changes |
| Imperative function | Performs one action, such as `updateRecord`, and returns a promise |
Exercise
Two components on one page both wire getRecord for the same account. An Apex method called from one of them changes the account's Industry on the server. Why might both components still show the old Industry, and what makes them refresh?
Show answer
LDS did not perform the change, so the cached record is unchanged and both wires keep serving the cached values. Calling getRecordNotifyChange with the record ID marks the cached record as stale, and LDS reloads it and sends the new values to both wires.
Exercise
Why does a component that wires getRecord usually cause no extra server request when another component on the page has already loaded the same record and fields?
Show answer
LDS keeps one shared cached copy of each record. It serves the already cached fields from memory and only requests fields that are missing.