Syncing in an Android Client
To use sync in an Android client app:
- Define a data model that extends AKBasicModel
- Create a schema for that data model
- Get an instance of the persistence manager
- Call the persistence manager's
- Handle the results of the
This document shows how to perform these tasks.
The following example defines a synchronizable class named Customer as a subclass of
AKBasicModel with three properties named named
custid. In this example,
custid contains the primary key as set by the server:
Shown below is the XML file containing the schema definition for the Customer class defined above. Notice that the
custid property has the
isKey attribute set to
TRUE to indicate that it contains the server's primary key:
Notice that this schema does not define an index field. By default, the key field is used as the index.
The field names in the model and in the corresponding schema definition do not need to match the corresponding names of the fields on the back end data store. However, you must adhere to any contract that you might have with the app scope so that your field names can be matched to those that the Akula project developer expects.
The following example shows how you add the synchronization mechanism to an Android app that uses the persistent data store. In this example, you perform the initial sync to download data from the Akula Server for the first time:
This example first calls the
initSchema() method to initialize the persistent data store with the schema of the sync data, and also to specify the endpoint of an app scope on the Akula Server that handles the sync. Notice that you only specify part of the full URL of the endpoint. That is because you must have already called the
AK.init() method with the URL of the app scope. For example, you might have already made the call:
This form of the
As you can see in this example, you only have to add a call to the
AKPersistenceManager.sync() method to synchronize the data with the back end data store. This example uses the InitSyncCallback class, an implementation of
AKSyncCallback, to handle the success and failure of the initial sync.
The next example calls the
AKPersistenceManager.sync() method to upload any local changes to the Akula Server and to download any remote changes to update the local data:
Notice that this call to the
AKPersistenceManager.sync()method uses a different callback class, UpdateSyncCallback, to handle the sync results. While it is not required, a client app often performs different operations for a sync update than it does for the initial download.