Imagine that you are developing an application that enables your users to store their data in the Cloud. Imagine that you want to let them work with it while no Internet connection is available. Imagine also that you want to let your users synchronize their data between different Windows Phone and Windows 8 devices so that they can consume it however and wherever they want. These are scenarios that are very common but implementing them requires writing serious amounts of code to make sure the data is correctly kept in sync across the multiple devices.



Now, imagine that you cover all of the above cases with a single line of code. Q2 2013 starts an entirely new wave of components for Windows Phone that we at Telerik have prepared to simplify connecting your app to the Cloud.

What's this all about?

The Cloud Data Sync mechanism is a backend component that allows
you to consume Cloud objects, store them locally, work with them in Offline Mode and be sure that they are kept in sync with their Cloud counterparts. You will not have to write complicated synchronization routines since everything you need is exposed by a simple API that allows you to:

  • Create new cloud objects
  • Delete existing cloud objects
  • Initiate a synchronization procedure when online to make sure everything is kept in sync

How it works?

The Cloud Data Sync mechanism is designed to work with any type of Cloud Service or a Local Storage Provider. In the CTP version we ship support for SQLite for storing objects locally and the Telerik Cloud Services for storing objects in the Cloud. The whole functionality is accessible through a single class called SynchronizationContext. It is easily initialized with a couple of lines of code as shown below:

SynchronizationContextPool.RegisterContextForType<Task>(new EverliveSyncServiceProvider<Task>("local_db"));
this.tasksContext = SynchronizationContextPool.GetContextForType<Task>();

Whereby the Task class looks like following:

public class Task : SynchronizableDataItem
{
    private string name;
    private string description;
    private DateTime dueDate;
  
    public string Name
    {
        get
        {
            return this.name;
        }
        set
        {
            if (this.name != value)
            {
                this.name = value;
                this.OnPropertyChanged("Name");
            }
        }
    }
  
    public string Description
    {
        get
        {
            return this.description;
        }
        set
        {
            if (this.description != value)
            {
                this.description = value;
                this.OnPropertyChanged("Description");
            }
        }
    }
  
    public DateTime DueDate
    {
        get
        {
            return this.dueDate;
        }
        set
        {
            if (this.dueDate != value)
            {
                this.dueDate = value;
                this.OnPropertyChanged("DueDate");
            }
        }
    }
}

Creating deleting objects and synchronizing is done with the SynchronizationContext:

SynchronizationContext<Task> tasksContext = SynchronizationContextPool.GetContextForType<Task>();
Task task = new Task();
task.Name = "Meeting with John";
task.Description = "Meet with John to discuss the sales statistics from the last month.";
task.DueDate = new DateTime(26, 12, 2013);
  
// Registers adds the item to the context. This stores is locally and schedules the item for server creation during the next synchronization routine
await tasksContext.AddAsync(task);
  
// Starts a new synchronization routine which makes sure local and Cloud items of the corresponding type are synchronized
await tasksContext.SynchronizeAsync();
  
// Marks the given object as deleted. It will be permanently deleted during the next synchronization routine.
await tasksContext.DeleteAsync(task);

or by using the methods exposed by the synchronizable objects:

// Deletes the task
task.Delete();
  
// Synchronizes the task
task.SynchronizeAsync();

To make it easier for you to get started, we have prepared a special Visual Studio Wizard which creates a Windows Phone application that has the Telerik Cloud Synchronization mechanism integrated. The application represents a simple TODO list application that allows multiple users and manages the TODO items for each user via the Telerik Cloud Synchronization mechanism.

Where can I get it?

You can find the Cloud Data Sync component  in the Q2 2013 release of RadControls for Windows Phone 8. It is currently in CTP so take a look at it and let us know what you think so that we can shape it up to perfection.

Help resources with step-by-step examples and code snippets are available online.


Deyan Ginev
About the Author

Deyan Ginev

Deyan has been with Telerik for more than ​six years working on several different products with main focus on mobile technologies. Deyan is currently working on NativeScript– a platform for developing native mobile apps with JavaScript. Find him on Twitter via
@deyan_ginev.

Related Posts

Comments