Data Synchronization: Creating a Sync Profile and Synchronizing Device Data
This tutorial demonstrates how you can synchronize device data, such as contacts and calendar events, with the OMA DS server.
This feature is supported in mobile applications only.
Warm-up
Become familiar with the Data Synchronization API basics by learning about:
-
Creating a Sync Profile
Check whether the device has a free slot for a new sync profile and create the profile.
-
Starting and Monitoring Data Synchronization
Start the synchronization operation and receive notifications about its progress.
Creating a Sync Profile
To be able to synchronize your device data, such as contacts and calendar events, with the OMA DS server, you must learn how to create a sync profile:
-
Check whether there are available profile slots on the device:
var numMaxProfiles = tizen.datasync.getMaxProfilesNum(); var numProfiles = tizen.datasync.getProfilesNum();
-
Create a sync profile. Use the SyncInfo, SyncServiceInfo, and SyncProfileInfo interfaces to provide sync profile and operation information:
if ((numMaxProfiles <= 0) || (numProfiles < numMaxProfiles)) { var syncInfo = new tizen.SyncInfo("http://example.com/sync", "myId", "myPassword", "MANUAL", "TWO_WAY"); var contactInfo = new tizen.SyncServiceInfo(true, "CONTACT", "serverContact"); var eventInfo = new tizen.SyncServiceInfo(true, "EVENT", "serverEvent"); var serviceInfo = [contactInfo, eventInfo]; var profile = new tizen.SyncProfileInfo("MyProfile", syncInfo, serviceInfo);
-
To be able to use the created profile, add it to your device using the add() method of the DataSynchronizationManager interface:
tizen.datasync.add(profile); var profileId = profile.profileId; }
Starting and Monitoring Data Synchronization
Learning how to start and monitor the data synchronization process is a basic data management skill:
-
Define the event handlers for the notifications using the SyncProgressCallback listener interface:
var syncCallback = { onprogress: function(profileId, serviceType, isFromServer, totalPerType, syncedPerType) { console.log('Total: ' + totalPerType + ', synced: ' + syncedPerType + ' for the sync type: ' + serviceType); }, onfailed: function(profileId, error) { console.log('Failed with id: ' + profileId + ', error name: ' + error.name); } };
-
Start the sync operation using the startSync() method, with the corresponding listener as a parameter:
tizen.datasync.startSync(profileId, syncCallback);