Syncing in an iOS Client
To use sync in an iOS 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 sync
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 iOS 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:forClass:syncEndpoint: method of
AKPersistenceManager 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
initialize: method of the AK class with the URL of the app scope. For example, you might have already made the call:
As you can see in this example, you only have to add a call to the
syncClass:forCallback:syncType: method to synchronize the data with the back end data store. This example uses the InitSyncCallback class, a subclass of
AKSyncCallback, to handle the success and failure of the initial sync.
The next example calls the
syncClass: 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
syncClass: 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.