Saving data locally to your device requires placing the data you want in a collection. The SaveData() function saves collections and LoadData() reloads them, so we can collect data in a collection and save it to the device while the device is offline.
If you leave the app and come back before connecting to a data source, you can use the LoadData() function to load the collection back to the device from the last time you ran SaveData().
Adding more gummy bear flavors to the same bag
In this example, we’re adding a new gummy bear flavor to a bag of gummy bears. First, we check to see if we’re connected. If we are, we create/update to the data source. If not, we create/update to a collection instead.
In this screen, we’re patching to the GummyBear SQL database — if we’re not connected, then we collect to the GummyOffline collection.
EX for Adding New Flavor: If( Connection.Connected, Patch( ‘[dbo].[GummyBear]’, Defaults( ‘[dbo].[GummyBear]’), { GummyBearFlavor : GummyFlavor_txt.Text, Qty : GummyQTY_txt.Text}), Collect( GummyOffline, { Flavor : GummyFlavor_txt.Text, Quantity : GummyQTY_txt.Text,Type : “New” }); SaveData( GummyOffline, “GummyOffline”))
We’re adding a field to the GummyOffline collection called Type. This flags the entry as a new flavor entry for the bag or updates the quantity of a flavor already in the bag. This will be important later, when we upload the offline data manipulation to the database.
We want to add a Passion Fruit flavor to the bag. Since we’re offline, we collect it in the GummyOffline collection, then we save the collection to the device with the file name of “GummyOffline”.
Since we added 15 gummy bears in a new flavor, we have to decrease the quantity of the other gummy bears in the bag to make room. So, we update the apple, cherry, and raspberry flavors with quantities decreasing the previous amount by 5.
Our QTYs should look like this:
Apple : 15
Cherry : 20
Raspberry : 15
EX for Updating QTYs: If( Connection.Connected, Patch( ‘[dbo].[GummyBear]’, First( Filter( ‘[dbo].[GummyBear]’, GummyBearFlavor = GummyFlavor_txt_1.Text)), { Qty : GummyQTY_txt.Text}), UpdateIf( GummyOffline, Flavor = GummyFlavor_txt_1.Text, { Quantity : GummyQTY_txt_1.Text, Type : “Update”}); SaveData(GummyOffline,”GummyOffline”))
Updating the database from the local device
We have the desired data with for the new Passion Fruit flavor, but only on our local device. How do we update the database with this information?
Once you’re connected, do a batch Patch to sync the data in the collection to the database, using the ForAll() function. Having icons with different colors is an easy way to make sure you’re. Like so:
EX for Connection notification:
If( Connection.Connected, RGBA(54, 176, 75, 1) ,RGBA(255, 0, 0, 1))
The ForAll() in our gummy bear example looks like this:
This where we need the Type field we added earlier. If this were an update to an already existing record, then Type would flag the change as an “Update” (Type flags new records as “New”). Once we run this ForAll() function, our updated database will hold the desired data input on a device while it’s offline.
EX for using the Type field: ForAll( GummyOffline, If( Type = “Update”, Patch( ‘[dbo].[GummyBear]’, First( Filter( ‘[dbo].[GummyBear]’, Flavor = GummyBearFlavor)), { Qty : Quantity}), If( Type = “New”, Patch( ‘[dbo].[GummyBear]’, Defaults( ‘[dbo].[GummyBear]’), { GummyBearFlavor : Flavor, Qty : Quantity}))))
As you can see below, the updated data on the new flavor and resulting quantities are in the bag.