cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
BenGillard
Level: Powered On

Offline Functionality

I currently have an app that allows users to input lots a data which they then hit submit which adds the record to SP List;

The formula for this is

Patch(PIOT,Defaults(PIOT),Form2.Updates,Form3.Updates,Form4.Updates,Form5.Updates)

this works fine but Is there a way i can extend this so if the device has no connectivity it will hold the information and will submit once connectivity is re established?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User
Super User

Re: Offline Functionality

There is a lot involved in setting up Apps to work Offline. I started a short explanation with rapidly grew to the below post, and even that is scratching the surface:

 

To work offline and store data offline, you can use the SaveData and LoadData commands to store/read data and the Connection.Connected object to determine if you are currently online/offline.

Some things to note:

  1. Offline working is broken on the Windows PowerApp Player and has been for several months (when offline, none of your previously downloaded Apps are shown for you to start). It is working on Android and iOS, but if you have Windows tablets, you care currently stuffed. 
  2. The SaveData command will generate errors when working in a Browsera work-around is to run the following code when starting the app:
Set(
    varIsSaveDataSupported,
    CountRows(
        SaveData(
            [
                "1",
                "2"
            ],
            "myTestList"
        ).Value
    ) = 2

You can then check the variable for whether the SaveData command is supported and only run the SaveData command if it is. This way, when working in a browser, you will only get the one error message on startup.

  1. You need to handle all the syncing of the data back to the datasource yourself.

 

How I typically handle this:

  1. When the application starts, I check whether it is online/offline. If online, I cache the data from my datasources to local collections andalso using SaveData.

 

ClearCollect(
            colMyRecommendation,
            Filter(
                Recommendation,
                UserGUID = Text(varCurrentUserID)
            )
        );
        If(
            varIsSaveDataSupported,
            SaveData(
                colMyRecommendation,
                "sdMyRecommendation"
            )
        )
  1. If the application is running offline, populate the local collections using LoadData.
  2. Throughout the App, save to the local collections using Path and then use SaveData to save the colleciton on the device.
  3. At appropriate times (e.g. starting the App, submitting a 'survey', or whatever) run a 'sync' of data if you are online - assuming you are using SharePoint, any record where IsBlank(ID) will be a new record. You can Filter for these and Collect them to the online datasource, e.g.
     Collect(Filter(colLocal, IsBlank(ID)),MyList)

When modifying an existing record I change the ModifiedDate to 1 Jan 2000. I can then filter for all records in the collection with that modified date and use a ForAll to Patch the changes back to the datasource.

At this time, I haven't had to deal with deleted records Smiley Happy but you could do similar to the ModifiedDate approach (choose a different date to indicate deletion, filter that out when accessing your collections, use ForAll to process all deleted records using the Remove command to delete from the datasource).

Below is sample code for syncing new and changed records for a table:

 

Collect(
    'General Customer',
    Filter(
        colMyGeneralCustomers,
        IsBlank(ID)
    )
);
ForAll(
    RenameColumns(
        Filter(
            colMyGeneralCustomers,
            Modified = DateValue("1 Jan 2000")
        ),
        "Title",
        "Updated_Title",
        "CustomerAddress",
        "Updated_CustomerAddress",
        "CustomerFirstName",
        "Updated_CustomerFirstName",
        "CustomerLastName",
        "Updated_CustomerLastName",
        "CustomerEmail",
        "Updated_CustomerEmail",
        "CustomerMobile",
        "Updated_CustomerMobile",
        "CustomerGUID",
        "Updated_CustomerGUID"
    ),
    Patch(
        'General Customer',
        LookUp(
            'General Customer',
            CustomerGUID = Updated_CustomerGUID
        ),
        {
            Title: Updated_Title,
            CustomerAddress: Updated_CustomerAddress,
            CustomerFirstName: Updated_CustomerFirstName,
            CustomerLastName: Updated_CustomerLastName,
            CustomerEmail: Updated_CustomerEmail,
            CustomerMobile: Updated_CustomerMobile
        }
    )
)

You'll need to think about any possible sync conflicts (e.g. record changed in App while offline and also directly in SharePoint).

You will also need to allow plenty of time for testing all the different online/offline scenarios.

In short - 'Offline mode' is not a simple switch in PowerApps - it requires a lot of extra development and testing!

 

Good luck!

1 REPLY 1
Highlighted
Super User
Super User

Re: Offline Functionality

There is a lot involved in setting up Apps to work Offline. I started a short explanation with rapidly grew to the below post, and even that is scratching the surface:

 

To work offline and store data offline, you can use the SaveData and LoadData commands to store/read data and the Connection.Connected object to determine if you are currently online/offline.

Some things to note:

  1. Offline working is broken on the Windows PowerApp Player and has been for several months (when offline, none of your previously downloaded Apps are shown for you to start). It is working on Android and iOS, but if you have Windows tablets, you care currently stuffed. 
  2. The SaveData command will generate errors when working in a Browsera work-around is to run the following code when starting the app:
Set(
    varIsSaveDataSupported,
    CountRows(
        SaveData(
            [
                "1",
                "2"
            ],
            "myTestList"
        ).Value
    ) = 2

You can then check the variable for whether the SaveData command is supported and only run the SaveData command if it is. This way, when working in a browser, you will only get the one error message on startup.

  1. You need to handle all the syncing of the data back to the datasource yourself.

 

How I typically handle this:

  1. When the application starts, I check whether it is online/offline. If online, I cache the data from my datasources to local collections andalso using SaveData.

 

ClearCollect(
            colMyRecommendation,
            Filter(
                Recommendation,
                UserGUID = Text(varCurrentUserID)
            )
        );
        If(
            varIsSaveDataSupported,
            SaveData(
                colMyRecommendation,
                "sdMyRecommendation"
            )
        )
  1. If the application is running offline, populate the local collections using LoadData.
  2. Throughout the App, save to the local collections using Path and then use SaveData to save the colleciton on the device.
  3. At appropriate times (e.g. starting the App, submitting a 'survey', or whatever) run a 'sync' of data if you are online - assuming you are using SharePoint, any record where IsBlank(ID) will be a new record. You can Filter for these and Collect them to the online datasource, e.g.
     Collect(Filter(colLocal, IsBlank(ID)),MyList)

When modifying an existing record I change the ModifiedDate to 1 Jan 2000. I can then filter for all records in the collection with that modified date and use a ForAll to Patch the changes back to the datasource.

At this time, I haven't had to deal with deleted records Smiley Happy but you could do similar to the ModifiedDate approach (choose a different date to indicate deletion, filter that out when accessing your collections, use ForAll to process all deleted records using the Remove command to delete from the datasource).

Below is sample code for syncing new and changed records for a table:

 

Collect(
    'General Customer',
    Filter(
        colMyGeneralCustomers,
        IsBlank(ID)
    )
);
ForAll(
    RenameColumns(
        Filter(
            colMyGeneralCustomers,
            Modified = DateValue("1 Jan 2000")
        ),
        "Title",
        "Updated_Title",
        "CustomerAddress",
        "Updated_CustomerAddress",
        "CustomerFirstName",
        "Updated_CustomerFirstName",
        "CustomerLastName",
        "Updated_CustomerLastName",
        "CustomerEmail",
        "Updated_CustomerEmail",
        "CustomerMobile",
        "Updated_CustomerMobile",
        "CustomerGUID",
        "Updated_CustomerGUID"
    ),
    Patch(
        'General Customer',
        LookUp(
            'General Customer',
            CustomerGUID = Updated_CustomerGUID
        ),
        {
            Title: Updated_Title,
            CustomerAddress: Updated_CustomerAddress,
            CustomerFirstName: Updated_CustomerFirstName,
            CustomerLastName: Updated_CustomerLastName,
            CustomerEmail: Updated_CustomerEmail,
            CustomerMobile: Updated_CustomerMobile
        }
    )
)

You'll need to think about any possible sync conflicts (e.g. record changed in App while offline and also directly in SharePoint).

You will also need to allow plenty of time for testing all the different online/offline scenarios.

In short - 'Offline mode' is not a simple switch in PowerApps - it requires a lot of extra development and testing!

 

Good luck!

Helpful resources

Announcements
firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

SecondImage

PowerApps Monthly Community Call

Next Wednesday, September 18th at 8am PDT

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

SecondImage

Power Platform Summit North America

Register by September 5 to save $200

Top Kudoed Authors
Users Online
Currently online: 116 members 4,337 guests
Please welcome our newest community members: