Cross-Platform Data Synchronization
Cross-Platform Data Synchronization
Data Synchronization
Dan Grover
Wonder Warp Software LLC
Good morning. I’m going to talk today about how you can write your own cross-plaform data
synchronization as part of your iPhone apps.
Outline
1 Why Syncing Is Important
2 Syncing Through The Ages
and why you still might want to write your own
Last November, I get an email from a friend of mine involved in the local Mobile Monday
group here in Boston. They were going to do a fancy event at the Omni Parker House on “up
and coming” mobile companies. Unfortunately, they couldn’t find enough up and coming
mobile companies, so they asked me to present instead.
At that time, I was mostly focused on Mac software -- I had a game out, but nothing much.
So I said “Oh, of course, I can demo the new iPhone version of ShoveBox”
Unfortunately, there was no iPhone version of ShoveBox. I didn’t really want to do one. It was
kind of beyond the scope of the app. And syncing was HAAARRRD.
So I made up a functional prototype of the iPhone app. I added a pretend dialog to the Mac
app to show it syncing. I had a script that I used to convert the example data over so it looked
the same.
?
Friday, October 16, 2009 7
I actually *did* want to make the iPhone version for real, though. But I had no idea how it was
going to work. I played around with a few half-way solutions -- storing the new entries and
just propagating those on sync. But I realized that real, honest-to-god two-way syncing was
doable if I just sat down and thought about it for a while. I studied all the ways that people
are doing syncing and realized it wouldn’t be too hard to write my own from scratch. Sounds
crazy.
Friday, October 16, 2009 8
A few months later, I finally ship the iPhone version. Sales quadruple, it gets two reviews in
Macworld. Still some bugs with syncing, but eventually those get ironed out.
Quick Demo
I’m going to get on my soapbox for a moment and explain briefly why I think this is an
important topic, and how it’s applicable to more apps than you’d think.
Friday, October 16, 2009 12
Syncing has been something people have been trying to solve for a long time.
If you follow the current hype, we don’t have to worry about it because...
the
CLOUD
...you put everything on the cloud! The cloud will solve all our problems!
The popular conception of the trend of “cloud computing” is a little wrong. People think of it
as a monolithic thing.
Friday, October 16, 2009 14
But the reality is that the huge benefit of cloud computing is that you can outsource the right
things to the right people. I use one company for sending my email newsletters, because they
have the best infrastructure and software for that. I use another for my regular web hosting,
and yet another to host downloads. And I use a help desk app called Zendesk. So it’s not
really on “the cloud” -- it’s on a lot of clouds!
So we’re back to the same problem -- data is going to be in a ton of different places, and you
have to build systems that can deal with that. Sync plays a big part.
A
CLOUD
A
CLOUD
CLOUD A
CLOUD
Friday, October 16, 2009 15
So the future’s more complicated than it seems. It’s not “the cloud”, but lots of clouds and
client apps and platforms and apparently goats. And they all have to be share data but
operate independently.
Does your app pass the
Green Line Test?
And if you don’t think data synchronization applies to your app, I’d like you all to try this
while you’re in the city. I call it the Green Line Test.
Friday, October 16, 2009 17
I used to live near Lechmere in East Cambridge, and I’d commute in to classes at
Northeastern using the Green Line. The Green Line touches a lot of areas of Boston and goes
above ground and below. Some of the stations underground are dead, some have reception.
Inevitably, the ones that the train stops inexplicably for 20 minutes in will be those that
don’t. You see, they’ve upgraded all the trains and haven’t quite got all the kinks worked out.
If your app is one of those “thin” or “hybrid” apps that needs to make an HTTP request to do
anything, you should try running your app for the entirety of a Green Line ride. How does it
handle it when you lose connectivity for a minute? Pop up an error? Or stall indefinitely? How
good an experience is it? Do you cache things well, or does it always need a connection?
If you find that it’s not very good in this situation, you should consider making more of your
application operate on the device itself, and then sync its state back to the cloud. It will be
more responsive and usable more of the time. You’ve probably avoided something like this
because, well, syncing is a pain. But what I’m going to talk about in this presentation will
help.
2
Syncing Through The Ages
and
I thought this tweet from Steven Frank was funny. It’s true. It never works.
I think that’s because there’s not a lot of knowledge about syncing out there. There are a lot
of companies that have written (bad) syncing, and a few academic papers on it. But not a lot
of talk about syncing as a subject. If more people didn’t have to waste all this time learning
the basics for themselves, we could have better syncing as more people work out the kinks
and integrate it in more systems.
Set-Reconciliation
Problem
Academics call syncing the “set reconciliation problem”. You’ve got two sets, and you want to
reconcile their differences. The literature on it is pretty limited though.
rsync
Subversion is a kind of syncing a lot of us probably use every day. Like most version control
systems, the idea is that your whole team can have the most current copy of the code.
Data ≠ Files
But it’s important to note that there’s a big difference between syncing *data* and syncing
*files*. Syncing data is a LOT harder!
DropBox
Dropbox is a consumer file syncing solution. But it actually ends up working a lot more like
Subversion than you’d think. It keeps revisions and actually handles conflicts in a neat way.
HotSync
Palm was one of the first companies to try to make a comprehensive syncing solution for
consumers.
The way HotSync works is that, once you’ve done the first sync, the Palm would set these
status flags on any piece of data that you changed. That would make it really fast to sync
back up with your PC, because the PC had an old copy of the data that both devices had the
last time you synced.
Mac OS X
Sync Services
Friday, October 16, 2009 26
Sync Services is Apple’s syncing framework. It’s pretty neat, and if you were like me and
trying to write a Mac app that synced with an iPhone app, it would *almost* work.
Sync Services
Macs
Sync Services has this concept of a “Truth Database” -- where you replicate all your data so
that it can sync it elsewhere. It gives you lots of goodies to sync your app to the Truth
database -- pushing and pulling changes. They give you tools to define the schema you want
the Truth to keep for your data.
But then it gets magically put on MobileMe and synced to other Macs. You don’t have any
control over that.
The iPhone supports MobileMe, but only for syncing contacts, appointments, and notes. It
doesn’t read in the truth database from Sync Services, it’s totally separate. There is no Sync
Services for the iPhone.
History-Based
Ex-Post-Facto
CONS CONS
Subversion Rsync
Dropbox Sync Services
HotSync (Fast) HotSync (Slow)
Architecture
So in these algorithms, we’re going to be a little abstract and think of this as two sets of data.
- A is all the data that’s on your first device, B is all the data that’s on the second device.
- Here’s all the data that’s *only on A*. That needs to be put on B if it was added, deleted
from A if not.
- Here’s all the data that’s *only on B*. That needs to be put on A if it was added, deleted
from B if not.
- Here’s the data that’s on both. This is the trickiest part. We need to sift through this data
and figure out if any of it has been modified since the last sync. We need to merge
modifications when we can, and otherwise, ask the user to resolve the conflict.
Friday, October 16, 2009 34
Goal of a Sync Algorithm
But there are a few ways to skin a cat. Let’s look at each of these. They all meet the definition
we discussed, but go about it differently.
Copy
A B
Good Will Hunting Good Will Hunting
With Honors
21 21
With Honors
21 21
created: 1PM
Boondock…
created: 1PM
Boondock… modified: 1PM modified: 1PM
So let’s go back here and talk about when to use each of these algorithms:
SYNC: This is what you’re going to want to do 95% of the time.
The other two algorithms are for when you’re first setting two devices up to sync.
COPY: Some people doing sync like to offer you a choice of data on either device to become
the “one true” set of data.
MERGE: What I do with ShoveBox is just do a merge the first time -- because there might be
data on both devices they want to keep. It avoids any confusion over the choice, and
nobody’s going to be pissed with the initial result.
Needed for Sync
•
Grab summaries from A and B
•
UUIDs, creation, modification
•
Sort into sets
skip it
•
else •
present conflict
•
end•
•
next
•
if c.count > 0 then
•
present conflict to user
•
e=a|b
•
end if
push e to a and b
next entry
Going Further
Syncer
Syncer
A B
Source Source
Friday, October 16, 2009 61
Architecture
Syncer
A B
LocalSource
SQLLite DB Source
Web Service
Friday, October 16, 2009 62
iPhone App
Architecture
Web Service
Friday, October 16, 2009 63
Architecture The Cloud
iPhone App
Web Service
Friday, October 16, 2009 64
Architecture
Mac App iPhone App
Syncer
A B
Source Source
Friday, October 16, 2009 66
Sync Source Abstraction
how to generate:
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
CFUUIDCreateString(kCFAllocatorDefault,uuid);
SBSyncEngine
SBSyncSource
SBIPhoneSyncSource SBLocalDBSyncSource
- (IBAction) start:(id)sender
- (IBAction) cancel:(id)sender;
- (NSDate *) lastSyncDate;
- (NSString *) currentlySyncingObjectName;
- (SBSyncEngineOperation) operation;
- (NSObject<SBSyncSource> *) sourceA;
- (NSObject<SBSyncSource> *) sourceB;
- (NSObject<SBSyncEngineDelegate> *) delegate;
- (void) setDelegate:(NSObject<SBSyncEngineDelegate> *)theDelegate;