2

I have a @wire that works well in most cases.

@track searchTerm = '';
@track searchType = 'All';
@track selectEvent = '' ; 
@track registrations;
@wire(searchRegistrations, {searchTerm: '$searchTerm', searchType: '$searchType', selectEvent: '$selectEvent'})
loadRegistrations(result) {
    console.log("loadRegist at work");
    this.registrations = result;
    }
} 

Changing searchTerm, searchType, selectEvent work as expected.

When changing searchType via a Button on the screen it brings up the resulting data by changing the '$searchTerm' wire parameter except if the key search fields use by the class is changed. The field is the assigned room. if we are

handleToggleClick() {
    // retrieve the classList from the specific element

    if (this.toggleIconName === 'utility:preview') {
        this.toggleIconName = 'utility:hide';
        this.toggleButtonLabel = 'View All Registrations';
        this.searchType = "NoRoomAssign";
    } else {
        this.toggleIconName = 'utility:preview';
        this.toggleButtonLabel = 'View not assigned Rooms ';
        this.searchType = 'All';
    }
}

I assign a room and save the record. Now if I click on the button and cause this.searchType = "NoRoomAssign"; If does not fire off the wire. I do not get the console.log in the loadRegistrations method.

Is this because the result is pulling from cache only even though the data is changed? How do I fix that?

Interesting blog about @wire but i'm unclear how to update the cache if that is what is needed. https://developer.salesforce.com/blogs/2019/09/caching-and-synchronizing-component-data-with-lightning-data-service.html

2
  • Did you try using the Refresh the Cache for a Wired Property? The example seems to suggest it would solve your use case, but I also know it can be tricky to get working: developer.salesforce.com/docs/component-library/documentation/… Commented Jan 28, 2020 at 21:40
  • following @Phil W suggestion I tried the imperative and got off the rails . Came back and tried the refresh Cache in the place that made the most sense and it worked great. I will convert to imperative when I need to touch this again.
    – Dan
    Commented Jan 29, 2020 at 23:01

1 Answer 1

0

Your bolded question is almost certainly the issue - if the specific set of parameter values have previously been used to invoke a wire and the results are still in the client-side cache then LWC automatically optimizes the interaction with the server and does not cause any call to be made. You can observe this using the browser's network inspector.

If you need to guarantee that a call is made to your Apex method you need to change from using a wire to using an imperative call and make sure that the Apex method is not cacheable.

This means writing more code in the LWC in order to handle changes to the properties that are the parameters. You no longer use tracking, but instead keep these as private properties. You then need to ensure that every piece of code that can change the values of these parameters invokes a function that in turn performs the imperative call.

Thus you might have:

_searchTerm = "";
_searchType = "All";
etc.

handleToggleClick() {
    // retrieve the classList from the specific element

    if (this._toggleIconName === 'utility:preview') {
        this._toggleIconName = 'utility:hide';
        this._toggleButtonLabel = 'View All Registrations';
        this._searchType = "NoRoomAssign";
    } else {
        this._toggleIconName = 'utility:preview';
        this._toggleButtonLabel = 'View not assigned Rooms ';
        this._searchType = 'All';
    }

    this.callSearchRegistrations();
}

callSearchRegistrations() {
    searchRegistrations({searchTerm: this._searchTerm, searchType: this._searchType, selectEvent: this._selectEvent})
        .then(result => {
            // Handle the success response
        })
        .catch(error => {
            // Handle the error response
        });
}

Note that it is possible to stick with a wire/cached AuraEnabled method and to force clear the cache using refreshApex each time one of the parameters for the wire changes (you must store the "result" value received in the wire handler for that call) though to me this is an abuse of the wire feature. The correct approach is to use an imperative call to a non-cached method.

3
  • Is there a way to flush the cache?
    – Dan
    Commented Jan 29, 2020 at 17:25
  • Yes, using refreshApex. However, you will need to know when to invoke this method (before each wire invocation) and that basically means you might as well switch to imperative apex, which will then be cleaner and easier to understand.
    – Phil W
    Commented Jan 29, 2020 at 19:07
  • refreshApex worked great while refreshApex is in the near future. thnks
    – Dan
    Commented Jan 29, 2020 at 23:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .