Why must an Apex method be cacheable=true to be used with @wire, and what are the restrictions?
Answer
@wire may call the method many times (reactive re-provisioning), so Salesforce requires it to be side-effect free and caches the result on the client.
Restrictions of @AuraEnabled(cacheable=true):
- No DML operations (insert, update, delete) in the method.
- Results are cached, so data may be stale until
refreshApexor a cache refresh.
If you need DML: call the Apex method imperatively without cacheable=true. If data may be stale after a DML, call refreshApex(this.wiredResult) where wiredResult is the stored wire response.
lwcapexcacheable