Apple Developer Connection
Advanced Search
Member Login Log In | Not a Member? Contact ADC

Integrating Sync Services into Your Application

Sync Services provides a powerful and extensible mechanism that enables data to move seamlessly between multiple Macs, devices such as mobiles phones and PDAs, and network services. This ability has long been a part of Mac OS X , through iSync and .Mac, which could manage contacts, calendar items, and bookmarks. Now in Mac OS X v10.4 Tiger, any Mac OS X application can tap into this functionality to sync any kind of data reliably, frequently, and easily. Tiger itself takes advantage of the Sync Services framework to synchronize Keychain items and Mail account settings between multiple Macs. The end result is that the data follows the user around and is always where they want it to be.

There are several ways in which you can develop with the Sync Services framework:

  • You can build into your application the ability to sync an existing data type, such as calendar items. This enables your application to utilize the same data that already appears in user's applications on other Macs and devices.
  • You can build the ability to sync a new data type so that those using your application on multiple Macs can always have up-to-date data no matter where they are.
  • You can build a sync client that enables a new device to sync existing or new data types. For example, a mobile phone manufacturer could provide a sync client that fully enables syncing data with a new model cell phone.

This article provides an overview of how Sync Services works and illustrates its multiple modes of operation. First, let's take a look at the architecture of the Sync Services framework.

Sync Services Architecture

Sync Services architecture

Sync Services is a based on a client-server model. The server, in this case, is the sync engine and the clients are the applications that utilize the capabilities of the framework. Each sync client is responsible for interpreting sync engine record formats into whatever form is needed by the application or device that client acts for. As well, each client can filter the data as needed to ensure that only the data that is applicable for an application or device is synchronized. The sync engine is flexible enough to support a diverse set of clients.

The sync engine performs the work of taking and merging changes from clients, and then computing changes to be communicated to all of the clients; and it can do so as frequently as is needed. The sync engine is also capable of supporting the requests of multiple clients at the same time; however, apps can synchronize independently of each other, allowing fast devices to sync frequently without being bogged down by slow devices.

Pushing and pulling changes

To ensure that all clients have the correct data, the sync engine maintains a truth database. This database contains the master copy of all of the information for the clients that use the Sync Services framework. When a client performs a sync with the sync engine, its data is synched with that of the truth database, not the data of any other client. This allows each client to maintain its own filtered view of the data without limiting the data that other clients use. The use of a master truth database also allows the sync engine to handle conflicts and duplicates as well as give clients only the changes that they need.

The sync engine is able to resolve most complex record changes and duplicates without requiring user input. It can handle changes to individual fields in each record according to its schema and not just to records as a whole. This means that if two clients modify different fields in a record, the engine can merge changes successfully without user intervention. Only in the case of a conflict at the field level will the sync engine require user intervention.

Sync Services Schemas

The data handled by the Sync Services framework must be described by a sync schema that details the specifics about the data handled by the sync engine. These models are entity-relationship models and follow the same concepts used by the Core Data framework. These concepts are:

Entity
An entity describes a discrete unit of data. For example, a person, a bookmark, or a to-do item. The name of each entity managed by Sync Services must be unique. For example, com.apple.contacts.PhoneNumber.
Attribute
An attribute represents a component of an entity. For example, a person entity would have a first name and a last name attribute. These are usually simple types like strings or numbers, or collection types such as arrays and dictionaries.
Relationship
A relationship describes an connection that an entity can have to other entities. These relationships can be optional or required as well as one-to-one or one-to-many. For example, a relationship can specify that a contact can have multiple phone numbers and require that every number have an associated contact.
Data Class
A data class is a logical grouping of related entities, typically one that makes sense to a user. For example, a group of contacts.

Sync schemas are defined in the property list format and are bundled in a .syncschema bundle, along with any other resources, such as images and localized string files associated with the schema. The internal structure of the sync services property list is shown in the following diagram.

Sync Services property list schema

Each type of data to be handled by Sync Services is represented by a separate sync bundle. For example, the three Sync schemas that ship with Tiger and define bookmarks, calendars, and contacts, are located in the /System/Library/SyncServices/Schemas directory.

You can take three approaches when working with Sync Services. The first is to simply use an existing schema. This approach works well when you are enabling a new device to work with Mac OS X or want to take advantage of existing types in your application. The second is to extend an existing schema with new types. For example, you might want to add fields to the system contacts schema so that your application can sync special attributes with multiple Macs. The third is to create an entirely new schema for new types.

Five Phases of Sync

In the simplest form of syncing, the client pushes state to the sync engine, the sync engine detects and applies those changes to the truth database, and then the client pulls changes back. To accomplish this, every sync operation goes through five phases. At each of these five phases a client can finish or cancel. These phases are:

  1. Create a Sync Session. When a client wants to push data to, or pull data from, the sync engine, it initiates a sync. Other clients that sync the same data type may be alerted by the sync engine to join the sync.
  2. Negotiate How You Will Sync. There are a variety of modes in which a client can sync with the sync engine. These are fast, slow, refresh, pull the truth, push the truth.
  3. Push Changes. Once a sync session has been negotiated, the client pushes the changes. It can either push just the deltas in each record or the whole record. If the whole record is pushed, Sync Services figures out the deltas.
  4. Resolve Changes. After the changes have been pushed by all of the clients participating in a sync session, the sync engine merges the changes from all clients into the truth database. If there are any conflicts, the sync engine attempts to resolve them and prompts the user for how to handle any unresolved conflicts.
  5. Pull Changes. After mingling the data, each client in a sync session pulls changes from the truth database as determined by the sync engine. Clients get the deltas and the full record and can then filter records that they don't want.

A sync operation takes place within the scope of an ISyncSession object. This object is a finite state machine that enforces the above steps in a transactional manner. Because of the underlying complexity associated with syncing, the Sync Services API is slightly more complex than some other Cocoa APIs. The order in which the methods are invoked, and the timing of those invocations, is important. It is also important to use the API correctly; syncing done incorrectly can result in customer data loss.

Sync Modes

The mode in which a sync occurs is a negotiation between client and the sync engine. This is because the process can involve multiple clients, and the final results of the sync depend on the state of all processes involved in the sync. A client is not necessarily given the sync mode it requests. Instead, the sync engine may force another mode depending on the needs of other clients in the sync.

The first time a client syncs, it performs a slow sync that pushes all of its records into the sync engine and pulls all of the changes. During the course of a slow sync, the sync engine keeps track of the client's state using a snapshot so that subsequent syncs can be more efficient. This allows the next sync that a client performs to be a fast sync.

It should be transparent to the user whether a sync is being performed in fast or slow mode. However, to ensure maximum performance and responsiveness, and that a user's data is up-to-date across applications and devices, most clients should fast sync often and as a result of user changes.

Conclusion

As you can see, Sync Services allows you to provide solutions that make sure a user's data is always available to them, no matter where that user is or what device they may be using. It provides the hard logic of handling sync operations as well as conflict resolution and lets you just concentrate on the data to be synced. Using the Sync Services API requires care to perform the right actions at the right time so that user data is seamlessly synchronized, but the reward is that your user's computing experience is decoupled from any one machine or device.

How You Can Get Started

Getting started couldn't be easier. The first thing you should do, if you haven't already, is to become an Apple Developer Connection member. A free ADC Online membership provides access to updates to Xcode and other developer tools. The paid ADC Select and ADC Premier memberships go further by providing access to pre-release seeds of system software and development tools, CD and DVD-based monthly mailings of sample code and resources, technical support, and great discounts on new hardware.

Next, you'll want to set yourself up with Apple's developer tools suite and IDE, Xcode 2.0. Xcode ships as part of each and every copy of Mac OS X Tiger on the Install DVD. Just double-click on the Xcode 2.0 package on the DVD and the developer tools—as well as a set of example code projects and comprehensive documentation in the ADC Reference Library—will be installed on your system. The documentation and sample code will help you learn more about the technologies covered in this article.

For More Information

Posted: 2005-06-06