2. Core Data vs Server Database
1 Server + Multiple Clients
3. Requirements
• Handle multiple clients: iOS, Web, Desktop...
• Reduce traffic
• faster sync
• mobile traffic is expensive AND extra slow
• only transfer stuff you have to transfer
• Reduce the number of requests
• making multiple requests has a significant overhead
4. Client Model Server Model
... your stuff ...
UUID
created
modified
last_sync
is_deleted
... your stuff ...
UUID
created
modified
is_deleted
PER-ITEM SYNC
If you need to merge, use per-attribute modification date
metadata
5. Creating GUIDs
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
CFUUIDCreateString(kCFAllocatorDefault,uuid);
Probability of Clash
n = 70,368,744,177,664 = 246 => 0.0000000004 (4 × 10−10)
annual risk of being hit by a meteorite (est.) = 0.00000000006 (6 × 10−11)
6. Step 0: Preparation
•Request object metadata from server
•Request only after earliest last_sync
•Sort it into:
•Objects only in client
•Objects only in server
•Objects in client & server
7. Step 1: Objects Only in Client
if !o.is_deleted
send o to server
else
delete o from client
UPDATE CLIENT.O.LAST_SYNC
8. Step 2: Objects Only in Server
if !o.is_deleted
copy o to client
UPDATE CLIENT.O.LAST_SYNC
9. Step 3: Objects in Client & Server
if client.o.modified > server.o.modified {
if client.o.is_deleted {
server.o.is_deleted = YES
delete o from client
}
else
send client.o to server
}
UPDATE CLIENT.O.LAST_SYNC
10. if client.o.modified < server.o.modified {
if server.o.is_deleted
delete o from client
else
copy server.o to client
}
if client.o.modified == server.o.modified
UPDATE CLIENT.O.LAST_SYNC
11. • our conflict resolution strategy is based on
always choosing the last modified
• we could use last_sync to detect conflicts
• present conflicts to user?
• user per-attribute modified date to resolve
automatically?