Newer Version Available

This content describes an older version of this product. View Latest

Background Actions

An action can be marked as a background action. This is useful when you want your app to remain responsive to a user while it executes a low priority, long-running action.

The framework supports background actions as well as foreground actions, which are the default. Each background action is sent in its own request and is executed in the order that it’s received. This is different from foreground actions. Multiple queued foreground actions are batched in a single request to minimize network traffic.

When the server-side actions in the queue are executed, the foreground actions are executed first and then the background actions are executed. Background actions run in parallel with foreground actions and may come back in either order.

The framework throttles foreground and background actions separately. This means that the number of long-running background server-side actions running at a time can be controlled. Throttling is done automatically, it is not user controlled, and the number of background actions allowed is three. Even with separate throttling, background actions might affect performance in some conditions, such as if the browser is doing many fetches from servers.

To set an action as a background action, get an instance of that action object in JavaScript and call the setBackground() method. This example shows how foreground and background actions are executed.
1swfobject.registerObject("clippy.codeblock-0", "9");<!-- This component shows two buttons that triggers 
2     foreground and background actions.
3     The results of the actions are printed below the buttons. -->
4
5<aura:component controller="namespace.echoController">
6    <aura:attribute name="echoes" type="String[]"/>
7    <ui:button label="Foreground" press="{!c.doForeground}"/><br/>
8    <ui:button label="Background" press="{!c.doBackground}"/><br/>
9
10        <aura:iteration items="{!v.echoes}" var="echo">
11            <p>{!echo.text} : {!echo.state}</p>
12        </aura:iteration>
13</aura:component>
14
1swfobject.registerObject("clippy.codeblock-1", "9");/** Server-Side Controller **/
2public class echoController {
3    @AuraEnabled
4    public static String echo(
5        @Key("message") String  message
6       ) throws InterruptedException {
7        Thread.sleep(1000);
8        return message;
9    }
10}
11
1swfobject.registerObject("clippy.codeblock-2", "9");/** Client-Side Controller **/
2({
3    doForeground: function(cmp, event, helper) {
4        helper.echo(cmp, 'Foreground #1');
5        helper.echo(cmp, 'Foreground #2');
6    },
7    doBackground: function(cmp, event, helper) {
8        helper.echo(cmp, 'Background #1', function(action) {
9            action.setBackground();
10        });
11        helper.echo(cmp, 'Background #2', function(action) {
12            action.setBackground();
13        });
14        helper.echo(cmp, 'Background #3', function(action) {
15            action.setBackground();
16        });
17        helper.echo(cmp, 'Background #4', function(action) {
18            action.setBackground();
19        });
20    }
21})    
22
1swfobject.registerObject("clippy.codeblock-3", "9");/** Helper **/
2({
3    echo: function(cmp, message, callback) {
4        var action = cmp.get("c.echo");
5        action.setParams({
6            message: message
7        });
8
9        action.setCallback(this, function(response) {
10            var state = response.getState();
11            var text = response.getReturnValue();
12            var echoes = cmp.get('v.echoes');
13            echoes.push({
14                text: text,
15                state: state
16            });
17            cmp.set('v.echoes', echoes)
18        });
19
20        if ($A.util.isFunction(callback)) {
21            callback(action);
22        }
23
24        $A.enqueueAction(action);
25    }
26}))

When the doForeground and doBackground controller actions are triggered in that order, the echoes are printed like this.

  • Background #3: SUCCESS
  • Background #2: SUCCESS
  • Background #1: SUCCESS
  • Foreground #1: SUCCESS
  • Foreground #2: SUCCESS
  • Background #4: SUCCESS

Foreground actions run in the order they are queued. Background actions run three at a time, so only the first three is guaranteed to complete before the fourth one is fired.

When the doForeground and doBackground controller actions are triggered in that order, the echoes are printed like this.

  • Background #1: SUCCESS
  • Background #3: SUCCESS
  • Background #2: SUCCESS
  • Background #4: SUCCESS
  • Foreground #1: SUCCESS
  • Foreground #2: SUCCESS

When isBackground is true for an action, the action can’t be set back to a foreground action. In other words, calling setBackground to set it to false will have no effect.

Note

To mark a server-side action as a background action in Java, use the @BackgroundAction annotation at the method level on the controller.