Use Apex While Mobile and Offline

Use Apex-backed wire adapters and imperative Apex in your Lightning web components to call Apex methods in your org.

When you use Apex in an LWC Offline-enabled mobile app, there are considerations to keep in mind so that you make efficient use of network resources, data caching, and handle offline behavior correctly.

First, when the client device is online, Apex-based features of Lightning web components, including Apex continuations, “just work.” You can use all of the features as documented in Call Apex Methods in the Lightning Web Components Developer Guide.

When a client device is offline, Apex-based features can read data that was cached while online, but changes (writing data) can’t be saved back to the server. Nor can a change via Apex methods be enqueued as a draft into the Offline Queue. A Lightning web component that uses Apex must be prepared to handle a network connection error as a normal response, for both reading and writing operations.

Before you make plans to reuse existing Apex custom code in your offline features, read important details about offline caching for Apex in Apex Results Are Separate from Other Primed Data.

Important

  • Use Apex in Lightning Web Components While Online

    The essentials of using Apex within Lightning web components are described in “Call Apex Methods” in the Lightning Web Components Developer Guide. While Apex features behave as documented when a client device is online, there are additional features available within an offline-enabled mobile app.

  • Enable Caching of Apex Results

    To allow results of Apex calls to be saved for offline use, enable caching on Apex methods used in your offline-enabled mobile apps.

  • Apex Results Are Separate from Other Primed Data

    Apex results are saved in the durable store separately from data stored by built-in components, modules, and wires that use Lightning Data Service (LDS) to access data.

  • Understand Apex Behavior While Offline

    Additional features that are built into offline-enabled apps allow the app, including Lightning web components and even Apex, to continue to function. Knowing these features, and their limitations, is critical to writing LWCs that function well, even without a connection to the Salesforce service.

  • Additional Considerations for Apex in an Offline-Enabled Mobile App

    The following differences in behavior compared to Apex run from a browser-based connection apply to Apex when used in Lightning web components in an offline-enabled mobile app.

  • Additional Documentation for Apex in Lightning Web Components

    Learn more about how to use Apex, including continuations, from Lightning web components documentation resources.