Synchronization Management
You can manage a synchronization schedule for applications by using a UI application to request for sync jobs through the Sync Manager, and a service application to listen for the requests through the Sync Adapter. The service and UI applications must have the same package name.
The main features of the Tizen.Account.SyncManager namespace are:
-
Sync Adapter
-
Setting Sync Adapter callbacks
You can set the callback methods in your Sync Adapter service application that your client application can call to set up a sync operation.
-
-
Sync Manager
-
Defining a sync job
You can create a new sync job and add user data into it as either an account information instance or a data bundle.
-
Requesting an on-demand sync job
You can add an on-demand sync job for a one-time operation.
-
Requesting a periodic sync job
You can add a periodic sync job with a recurring cycle.
-
Requesting a data change sync job
You can add a data change sync job for receiving a notification whenever a specific database changes.
-
Retrieving all registered sync jobs
-
Removing sync jobs
You can remove registered sync jobs when they are no longer needed.
-
Prerequisites
To enable your application to use the synchronization management functionality, follow the steps below:
-
To use the Tizen.Account.SyncManager namespace, the application has to request permission by adding the following privileges to the
tizen-manifest.xml
file:XMLCopy<privileges> <privilege>http://tizen.org/privilege/account.read</privilege> <privilege>http://tizen.org/privilege/account.write</privilege> <privilege>http://tizen.org/privilege/alarm.set</privilege> <privilege>http://tizen.org/privilege/calendar.read</privilege> <privilege>http://tizen.org/privilege/contact.read</privilege> </privileges>
-
To use the methods and properties of the
Tizen.Account.SyncManager
namespace, include it in your application:C#Copyusing Tizen.Account.SyncManager;
NoteTo use the features of the Tizen.Account.SyncManager namespace, the service application must first set the callbacks. A UI application cannot initialize or set callback methods through the Tizen.Account.SyncManager.SyncAdapter class. Instead, the UI application must call the methods of the Tizen.Account.SyncManager.SyncClient class to request sync operations from the service application.
Set sync adapter callbacks
To set callbacks in your Sync Adapter service application that your UI application can call to request sync operations:
-
Set up event handlers for starting and stopping data synchronization. When the
StartSyncCallback()
callback of the Tizen.Account.SyncManager.SyncAdapter class is invoked, the predefined data sync process is performed inside the callback method. TheCancelSyncCallback()
callback works in a similar way and cancels the data sync process:C#Copystatic bool StartSyncCallback(SyncJobData data) { /// Code for starting data synchronization return true; } static void CancelSyncCallback(SyncJobData data) { /// Code for cancelling data synchronization }
-
Register the event callbacks with the
SetSyncEventCallbacks()
method of theTizen.Account.SyncManager.SyncAdapter
class to receive notifications regarding the sync operation.When a specific event is detected or a sync job is requested, the
StartSyncCallback()
orCancelSyncCallback()
callbacks are invoked:C#CopySyncAdapter obj = new SyncAdapter(); obj.SetSyncEventCallbacks(StartSyncCallback, CancelSyncCallback);
-
When the sync operations are no longer needed, unset the callbacks to free the
Tizen.Account.SyncManager.SyncAdapter
instance:C#Copyobj.UnsetSyncEventCallbacks();
Define sync job
To define a sync job, create a new Tizen.Account.SyncManager.SyncJobData instance:
C#
Copy
SyncJobData request = new SyncJobData();
request.SyncJobName = "PeriodicSyncJob";
You can add user data to a sync job as an account information instance or as a data bundle, follow these steps to add user data as an account information or as a data bundle:
-
To add account information to a sync job, create a new instance of the Tizen.Account.AccountManager.Account class. Add your account information to it, and then add it into the sync job as the
Account
property of theTizen.Account.SyncManager.SyncJobData
instance. For more information about creating accounts, see Creating and Managing an Account:C#Copyusing Tizen.Account.AccountManager; AccountManager.Account account = AccountManager.Account.CreateAccount(); account.UserName = "Kim"; account.SyncState = AccountSyncState.Idle; AccountService.AddAccount(account); SyncJobData request = new SyncJobData(); request.Account = account;
-
To add a data bundle to a sync job, create a new instance of the Tizen.Applications.Bundle class, add your data to it, and add it as the
UserData
property of theTizen.Account.SyncManager.SyncJobData
instance:C#Copyusing Tizen.Applications; Applications.Bundle bundle = new Applications.Bundle(); bundle.AddItem("key", "value"); SyncJobData request = new SyncJobData(); request.UserData = bundle;
Request on-demand sync job
To request a one-time sync job from the Sync Adapter service application, use RequestOnDemandSyncJob()
of the Tizen.Account.SyncManager.SyncClient class:
C#
Copy
SyncJobData request = new SyncJobData();
request.SyncJobName = "OnDemandSyncJob";
int id = SyncClient.RequestOnDemandSyncJob(request, SyncOption.NoRetry);
Request a periodic sync job
To register a periodically-recurring sync operation with the Sync Adapter service application, follow these steps:
-
To set up a periodic sync job with a regular sync interval, use
AddPeriodicSyncJob()
of the Tizen.Account.SyncManager.SyncClient class, and give the sync interval as a value of the Tizen.Account.SyncManager.SyncPeriod enumeration. In the following example, the sync interval is set to 30 minutes:C#CopySyncJobData request = new SyncJobData(); request.SyncJobName = "PeriodicSyncJob"; int id = SyncClient.AddPeriodicSyncJob(request, SyncPeriod.ThirtyMin, SyncOption.None);
You can also add additional parameters to the sync job using values of the Tizen.Account.SyncManager.SyncOption enumeration. The value
NoRetry
means that the application does not retry the sync job if it fails, andExpedited
means that the sync job is handled as soon as possible:C#Copyid = SyncClient.AddPeriodicSyncJob(request, SyncPeriod.OneHour, SyncOption.NoRetry); id = SyncClient.AddPeriodicSyncJob(request, SyncPeriod.OneDay, SyncOption.Expedited);
-
You can renew a previously registered periodic sync job with
AddPeriodicSyncJob()
by using the same Tizen.Account.SyncManager.SyncJobData instance with new parameters:C#CopySyncJobData request = new SyncJobData(); request.SyncJobName = "PeriodicSyncJob"; int id = SyncClient.AddPeriodicSyncJob(request, SyncPeriod.ThirtyMin, SyncOption.None); id = SyncClient.AddPeriodicSyncJob(request, SyncPeriod.TwoHours, SyncOption.Expedited);
Define a data change sync job
To register a data change sync job with the Sync Adapter service application, to occur whenever corresponding data changes, follow these steps:
-
Add a data change sync job with
AddDataChangeSyncJob()
of the Tizen.Account.SyncManager.SyncClient class. This method adds the sync job only for the capability given as the value of theSyncJobName
property of the Tizen.Account.SyncManager.SyncJobData instance. For example, to add a data change sync job for the calendar:C#CopySyncJobData request = new SyncJobData(); request.SyncJobName = SyncJobData.CalendarCapability; int id = SyncClient.AddDataChangeSyncJob(request, SyncOption.None);
You can also add additional parameters to the sync job using values of the Tizen.Account.SyncManager.SyncOption enumeration. The value
NoRetry
means that the application does not retry the sync job if it fails, andExpedited
means that another sync job is handled as soon as possible:C#CopySyncJobData request2 = new SyncJobData(); request2.SyncJobName = SyncJobData.ContactCapability; SyncJobData request3 = new SyncJobData(); request3.SyncJobName = SyncJobData.ImageCapability; int id2 = SyncClient.AddDataChangeSyncJob(request2, SyncOption.NoRetry); int id3 = SyncClient.AddDataChangeSyncJob(request3, SyncOption.Expedited);
-
You can renew a previously registered data change sync job with the
AddDataChangeSyncJob()
method by using the sameTizen.Account.SyncManager.SyncJobData
instance with new parameters:C#CopySyncJobData request = new SyncJobData(); request.SyncJobName = SyncJobData.ContactCapability; int id = SyncClient.AddDataChangeSyncJob(request, SyncOption.Expedited); id = SyncClient.AddDataChangeSyncJob(request, SyncOption.None);
Retrieve all registered sync jobs
To retrieve a list of all registered sync jobs, use the GetAllSyncJobs()
method of the Tizen.Account.SyncManager.SyncClient class:
C#
Copy
SyncJobData request = new SyncJobData()
{
SyncJobName = "PeriodicSyncJob"
};
int periodicId = SyncClient.AddPeriodicSyncJob(request, SyncPeriod.ThreeHours, SyncOption.None);
SyncJobData request2 = new SyncJobData()
{
SyncJobName = SyncJobData.MusicCapability
};
int dataChangeId = SyncClient.AddDataChangeSyncJob(request2, SyncOption.None);
IEnumerable<KeyValuePair<int, SyncJobData>> syncJobs = SyncClient.GetAllSyncJobs();
foreach (KeyValuePair<int, SyncJobData> item in syncJobs)
{
if (item.Key == periodicId)
{
Console.WriteLine(item.Value.SyncJobName.ToString());
}
if (item.Key == datachangeId)
{
Console.WriteLine(item.Value.SyncJobName.ToString());
}
}
Remove sync jobs
To remove registered sync jobs, use the RemoveSyncJob()
method of the Tizen.Account.SyncManager.SyncClient class, using the id
property of the job to be removed:
C#
Copy
SyncJobData request = new SyncJobData();
request.SyncJobName = "PeriodicSyncJob";
SyncJobData request2 = new SyncJobData();
request2.SyncJobName = SyncJobData.ImageCapability;
int id = SyncClient.AddPeriodicSyncJob(request, SyncPeriod.OneHour, SyncOption.Expedited);
int id2 = SyncClient.AddDataChangeSyncJob(request2, SyncOption.None);
SyncClient.RemoveSyncJob(id);
SyncClient.RemoveSyncJob(id2);
Related information
- Dependencies
- Tizen 4.0 and Higher