When building mobile apps, one of the most common scenarios app makers face is how to enable their users be productive in situations where there is limited or no connectivity at all. This has been one of the most requested features for PowerApps to allow running apps while being disconnected and to provide some support for offline data caching. In this release of PowerApps, we are delivering the first set of improvements for app makers to achieve that by enabling:
NOTE: The offline feature area is still under development and is not optimized for every offline scenario today. The functions to SaveData() to a local device and LoadData() from that device work best in their current implementation over relatively small quantities of data (e.g., dozens of text records in a table) that generally do not exceed 2MB. This is useful for some basic “offline” scenarios as well as to increase the startup performance of canvas apps by caching data locally. However, using this feature to save large amounts of data (e.g., saving thousands of rows in a table, or caching large images or videos) may cause errors or unexpected behavior with the current implementation and should be avoided. Also, the functions do not automatically resolve merge conflicts when a device returns to connectivity from offline – configuration on what data is saved and how to handle reconnection is up to the maker when writing expressions. We are working to expand the capabilities of offline apps to increase stability and size limits, and in the future to automatically handle decisions about what to save and how to handle conflicts. Stay tuned here and on the PowerApps blog for updates when they become available. For more advanced examples of PowerApps Offline scenarios please check out the following blog posts as well
How to build offline capable apps
The first thing to discuss is how PowerApps applications access data. The primary mechanism for PowerApps applications to consume data is through a set of connectors provided by the platform such as SharePoint, Office 365 and the Common Data Service. You can also build your own custom connectors that can allow you to build any custom logic and capabilities as well as run them anywhere (such as using Azure Functions). The connectors are accessed over the internet using HTTPS which means your users will need to be online for them to access this data or functions that they offer.
One of the interesting aspect of PowerApps is that it offers a set of capabilities and formulas that allow you to filter, search, sort, aggregate, insert and manipulate data that is consistent regardless of the data source such as if you are using a SQL database, a SharePoint List, a Common Data Service entity, or even collections that are locally stored the device. This allows us not only to easily build applications that can be retargeted to use a different backend but also that can be modified to use local collections instead with almost no changes to the logic. When dealing with offline data, local collections will be the primary mechanism that PowerApps offers today.
To keep the focus on the offline aspects and showing some of these new capabilities we are going to keep the scenario very simple. In this case, we are going to build an application that allows you to read twitter posts while being offline as well as tweet while being offline and when the application comes online the tweets will be posted and the local data will be reloaded.
At a high level, the application that we will be creating will do the following:
If(Connection.Connected, ClearCollect(LocalTweets, Twitter.SearchTweet("PowerApps", {maxResults: 100})); UpdateContext({statusText: "Online data"}) , LoadData(LocalTweets, "Tweets", true); UpdateContext({statusText: "Local data"}) ); LoadData(LocalTweetsToPost, "LocalTweetsToPost", true); SaveData(LocalTweets, "Tweets")
This formula checks if we are online, if we are it will Load into a “LocalTweets” collection up to 100 tweets with the search term “PowerApps”. If offline, it will load the local cache if available from a file called “Tweets”.
If (Connection.Connected, "Connected", "Offline")
This formula checks if the device is online, if it is the text of the label will say “Connected” otherwise “Offline”.
If (Connection.Connected, Twitter.Tweet("", {tweetText: NewTweetTextBox.Text}), Collect(LocalTweetsToPost, {tweetText: NewTweetTextBox.Text}); SaveData(LocalTweetsToPost, "LocalTweetsToPost") ); UpdateContext({resetNewTweet: true}); UpdateContext({resetNewTweet: false})
This formula checks if we are online, if we are it will tweet the text immediately. If we are not, it will capture the tweet in a “LocalTweetsToPost” collection and save it to the device. Then it resets the text in the text box.
If(Connection.Connected, ForAll(LocalTweetsToPost, Twitter.Tweet("", {tweetText: tweetText})); Clear(LocalTweetsToPost); Collect(LocalTweetsToPost, {tweetText: NewTweetTextBox.Text}); SaveData(LocalTweetsToPost, "LocalTweetsToPost"); UpdateContext({statusText: "Online data"}) )
This formula verifies if the device is online and if it is it will Tweet all the items that are in the “LocalTweetsToPost” collection. Then it will clear the list.
With this release of PowerApps you can now run PowerApps and launch apps even while being offline. However, to be able to run an application you must run it at least once while being online so we can download the app.
![]() |
![]() |
PowerApps running online | PowerApps running offline |
Hopefully this post gives you an idea of some of the initial capabilities that we are adding to enable building basic offline functionality to your application. We will continue to invest on making this scenario not only more streamlined but also more powerful over time. Some of the short term improvements that you can expect to see in a few weeks include:
We hope you will find this capability useful. Over time we will also invest on providing you with better ways to handle conflicts, error handling for network calls, and more.
Build offline apps with new PowerApps capabilities
原文:https://www.cnblogs.com/lingdanglfw/p/14626696.html