To demonstrate a specific issue with LDS cache, I have created simple lightning web component that displays list of accounts (filterable by account type) using
lightning-datatable. The source code for this component can be accessed in this git repo. [Since this is a demo component, I may have skipped best practices and detailed comments, but the code is fairly easy to understand.]. High level details of the component are given below:
- LWC uses
lightning-comboboxto display the account types and is used to filter the result set. Based on a user selected type value, a (un)filtered list of accounts is displayed in a
lightning-datatable. The account name field is editable in the data table.
- Component wires custom apex methods to fetch account types and list of accounts; and uses
lightning\uiRecordApito update changes made to the account name.
refreshApexmethod is used to force refresh on LDS cached result.
- Apex class has 2 methods - One for fetching the list of distinct Account Types and the other for fetching list of accounts based on the account type.
After updating a record, LDS cache is not refreshed as expected. Replication steps mentioned below:
- Note down the first 2 account records (account name) for the type
Customer - Direct.
Customer - Directin the combobox to get the filtered result set.
- Edit the account names for the records noted down earlier and click 'Save' to update the data.
- Component is refreshed with the latest updated data.
All Typesin the combobox to get list of all accounts. It can be observed that the updated records do not display the latest values (but old values from LDS cache).
- Switch back to
Customer - Directto see the updated values.
This indicates that there are distinct LDS cache entries for each filtered list of accounts and only the cache corresponding to the current displayed result set (or the one fetched by last @wire adapter execution) is refreshed. The scenario presented here is a very simple one, but there could be slightly complex scenarios. For example, the filter values are fetched from a multi-select picklist field and the record(s) could be present n (>2) number of filtered result sets displayed in the data table. This would require all n LDS cache entries to be refreshed.
What I already know or tried so far:
- Use imperative apex calls for accounts retrieval as well as updating and use
refreshApexto have complete control over the displayed data. By taking this approach, I might have to sacrifice the usage of
ui*Apimethods as well as LDS cache.
- Use the same approach as above, but mark apex methods as cacheable [
@AuraEnabled(cacheable=true)]. Doing this doesn't help either because this still doesn't refresh all the cache entries.
Solution: Work-around to solve this problem is provided as answer. But I would like to know if there is any better approach (or if I missed out on a really simple logic) to solve this problem.
Note: I've tried to provide as much details as I could to illustrate the problem. Feel free to comment, if any part of this question is unclear. Also, in the solution, I may have skipped some best coding practices since that's not the problem at focus. Suggestions are welcome.