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

< Previous PageNext Page > Hide TOC

Refresh Syncing

Your application needs to be robust enough to recover from unexpected events—for example, the absence of a local file. Suppose the user creates some data and syncs it, and for some unknown reason, removes the local file. At this point, the sync engine assumes that the client has copies of the records and will not send them to the client on the next sync. The client needs to explicitly request a refresh sync when this occurs; otherwise, records may be destroyed.

Contents:

Accidently Deleting Records
Requesting a Refresh Sync


Accidently Deleting Records

For example, if you create some records using SimpleStickies and remove the local file, you can lose all your records. Follow these steps to test if your application properly handles a missing local file.

  1. If you haven’t created any records with SimpleStickies, launch SimpleStickies, create some records, save (and sync) the file, and quit.

  2. Remove the com.mycompany.SimpleStickies.xml file, located in the ~/Library/Application Support/SyncExamples folder, which contains the local copy of the client’s records.

  3. Using Syncrospector, select Clients from the pull-down menu to view the state of SimpleStickies—select SimpleStickies from the table and click Sync State in the detail view.

    If SimpleStickies is expected to fast sync the next time it syncs, and it fails to load the local records when launching, it will fail to apply any changes pulled from the sync engine, except adds. If the application slow syncs the next time, SimpleStickies will not push any records, and the sync engine will assume that the client deleted those records. This may result in the unintentional loss of data.

  4. Force SimpleStickies to slow sync by selecting SimpleStickies from the list of clients and selecting Slow Sync from the right-hand side pull-down menu.

    When you click Sync State the Will Sync mode should read “slow.”

  5. Launch SimpleStickies again.

    SimpleStickies syncs immediately after launching. If FancyStickies is running, it will join the sync session.

  6. If a change alert panel appears in FancyStickies, click Allow to continue.

    Because SimpleStickies doesn’t push any records, the sync engine issues delete changes to FancyStickies and all its sticky windows disappear.

  7. Examine the Note records in the truth database by selecting Truth from the left-side pull-down menu in Syncrospector. The sync engine deleted all of the Note records.

    You will see that all the Note records have been marked for deletion.

To avoid this from happening in your application, you should request a refresh sync if you detect that the local file is missing—that is, remove all local records and “pull the truth.”

Requesting a Refresh Sync

The AppDelegate dataSource accessor method creates its data source by attempting to load the data from a file. You need to modify this implementation to set the preferred sync mode to refresh when the backup file doesn’t exist. You do this by sending setPreferredSyncMode: to the SyncIt object, passing RefreshSyncMode as the argument. Add this line to the dataSource method after the NSLog() function call:

        [_syncIt setPreferredSyncMode:RefreshSyncMode];

The code fragment should now look like this:

    NSURL *url = [NSURL fileURLWithPath:[applicationSupportFolder stringByAppendingPathComponent:[self fileName]]];
    if ([self _readFromURL:url] == NO) {
        NSLog(@"No data from backup file on init, creating empty new data source");
        [_syncIt setPreferredSyncMode:RefreshSyncMode];
    }
    else {
        NSLog(@"Read in some data on init from backup file");
    }

Now examine the SyncIt.m code to see how the preferred sync mode is used when syncing your application.

  1. Open SyncIt.m in Xcode.

  2. Search for RefreshSyncMode, and you will see this code fragment:

            if (_syncMode == RefreshSyncMode) {
                NSLog(@"Requesting refresh sync from engine for entity names %@", [_syncSource entityNames]);
                [_syncSession clientDidResetEntityNames:[_syncSource entityNames]];
            }
  3. Build and test the application. Create some records using SimpleStickies and then quit the application. Remove the local file and click Build and Go in Xcode to restart SimpleStickies.

Sending clientDidResetEntityNames: to the sync session notifies the sync engine that the client’s sync state should be reset. Consequently, the client will not pull delete changes for the records that it lost.

Follow the steps in “Accidently Deleting Records”to test if the changes to SimpleStickies, requesting a refresh sync, works.



< Previous PageNext Page > Hide TOC


Last updated: 2007-07-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice