Canvas Apps Performance

How to optimize fetching the same value again and again

A repetitive pattern of inhaling and exhaling

Introduction - Fetching same record multiple times

As makers, we fetch records into an app all the times. We reference some of these records at multiple places in an app. User record of the currently logged in user is probably the most common example. The simplest approach is to fetch it whenever and wherever needed using the following code:

Patch(
    Users,
    LookUp(
        Users,
        'Primary Email' = User().Email
    ),
    {'Mobile Phone': "1234567890"}
)

Or if you have followed my previous posts on performance, you would do something like this:

With(
    {
        locUser: LookUp(
            Users,
            'Primary Email' = User().Email
        )
    },
    Patch(
        Users,
        locUser,
        {'Mobile Phone': "1234567890"}
    )
)

That's 10 data calls if you lookup the user record at 10 different places in the app. Which is, in most cases, unnecessary.

Fetching a record once and reusing it

The simplest approach is to fetch the record once and store it in a variable. Depending on where you need the record, you could do this in the app's OnStart or the OnVisible of a splash screen.

Set(
    gblUser,
    LookUp(
        Users,
        'Primary Email' = User().Email
    )
)

Re-fetching a record when its updated

If you update the user record anywhere in the app, then you would want to re-fetch the record and store it in the variable, as shown below:

With(
    {
        locUser: LookUp(
            Users,
            'Primary Email' = User().Email
        )
    },
    Set(
        gblUser,
        Patch(
            Users,
            gblUser,
            {'Mobile Phone': "1234567890"}
        )
    )
)

Recent articles

  1. How to increase performance by trashing extra screens and controls
  2. How to get rid of cross screen dependencies
  3. Optimize data calls inside a gallery

Leave a Reply