Offline-Enable Your Android Application
- How the offline feature works, through demonstration
- How the synchronization code works
- How to handle errors that occur while syncing
- Step 1
-
Follow the instructions at Try Out the SAP BTP SDK Wizard for Android to create a new application using the SAP BTP SDK Wizard for Android and select Offline for the OData option on the Project Features tab. The push feature is not needed for this application.
-
Run the app. After the login process, a screen is displayed explaining that the offline store is opening. As the screen suggests, opening the offline store for the first time can take up to a few minutes. One technique to decrease this initial time is to only download data that is relevant to the user, such as customers that belong in their sales region.
-
When you get to the app’s home page, turn on airplane mode on your device, or disable Wi-Fi and data.
-
The entity list screen is populated based on the
metadata.xml
file retrieved when the application was created. Tap the Products list item.The Products screen makes a data request to display the available products. Notice that it succeeds without a working network connection. The data request is fulfilled from the offline store that was previously created and populated on the device. Tap the Accessories item to display the detail screen.
-
On the detail screen, tap the edit toolbar icon.
-
Make a change to the currency code and tap the save toolbar icon.
-
Navigate back to the app’s Home screen and tap Synchronize using the three-dot-menu in the top right of the title bar.
The sync should fail because you haven’t turned airplane mode off yet.
-
Turn off airplane mode or re-enable Wi-Fi/data and attempt a sync again. You will see a notification that describes the sync action.
When the sync completes, the change you made will have been applied to the back end.
Which of the following is true the first time an offline-enabled app is opened?
-
- Step 2
Make sure you are selecting the right language above.
To protect the data in offline store, you must enable offline store encryption by supplying an encryption key.
For additional information about multiple user mode, see Enable Multi-User Mode for Your Android Application.
- Step 3
Defining queries tell the
OfflineODataProvider
(the class that manages the offline store) which entity sets to store on the device. In the case of the wizard-generated application, there is a defining query for each available entity by default, meaning that each entity is stored offline and available if the user doesn’t have an internet connection. For more information, see Defining Queries.With an offline-enabled app, requests made against the entity sets that are included in the defining requests will always be fulfilled from the local offline store.
What is the purpose of a defining query?
- Step 4
The application allows users to make changes against a local offline store and synchronize manually at any time. The sync operation is performed by a worker. There are three operations that must be implemented in order to use the offline store functionality:
open
,download
, andupload
. As their names suggest, the operations open the offline store, download server changes, and upload user changes, respectively. In the wizard-generated application,OfflineOpenWorker
implementsopen
during initialization andOfflineSyncWorker
implements a sync operation, which requiresdownload
first, and thenupload
.For more information about how the offline store works, see the Working With Offline Stores.
What does the method synchronize do?
- Step 5
When syncing changes made while offline, conflicts can occur. One example might be if two people attempted to update a description field for the same product. Another might be updating a record that was deleted by another user. The
ErrorArchive
provides a way to see details of any of the conflicts that may have occurred. The following instructions demonstrate how to useErrorArchive
.-
Update a SalesOrderItem and change its quantity to be zero and save it. Update a second item and change its quantity to a different non-zero number and save it.
Notice that the items are now marked with a yellow indicator to indicate that an item has been locally modified but not yet synced.
-
Attempt a sync, and you’ll notice that the sync completes, but if you examine the SalesOrderItems list, one item has a red mark beside it, indicating it is in an error state. This is because the back end has a check that SalesOrderItems cannot have zero for their quantity. This check does not exist in the local offline store, so the update succeeds locally but fails when the offline store is synced.
-
- Step 6
In this section we will create an Error Information screen that displays the details from the
ErrorArchive
.Further information on using the offline feature can be found at Step by Step with the SAP BTP SDK for Android — Part 6 — Offline OData.
Congratulations! You have created an offline-enabled app using the SAP BTP SDK Wizard for Android and examined how the
ErrorArchive
can be used to view synchronization errors!