Players change phones and reinstall apps, and they expect their stars to come with them. We wanted cloud save in our new game from the first release, on both Android and iPhone, without a server and without a conflict dialog. The trick is a merge rule so simple that it fits in one line, and a shared piece of Kotlin that both platforms call.
Two stores, one format
On Android the progress lives in Play Games Saved Games. On iPhone it lives in the iCloud key-value store. They have nothing in common technically, so the shared code does not touch either. It only knows how to turn progress into a small text blob, how to read a blob back, and how to merge two of them.
The blob has one entry per line in the form key, a vertical bar, and an integer, sorted by key. Door stars, level stars, unlocked doors and one total counter go in. A reader skips anything it does not understand, negative numbers included, so a damaged blob can never crash the game.
Why iCloud and not Game Center
Both could store a save on iPhone. We chose the iCloud key-value store because it asks nothing of the player: if iCloud is on for the device, which is the default, it just works. Game Center saved games need a Game Center login that many players have never used, and a login dialog is friction exactly at the moment we want a smooth restore. Our data is only a few kilobytes and the key-value store allows far more, so size was never an issue.
The rule: the larger number wins
Merging two phones' progress sounds like a conflict problem until you look at what is being stored. Stars only go up. An unlocked door is zero or one and never goes back to zero. The total solved counter only grows. For values like that, taking the larger of the two always gives the right answer.
This kind of merge has three useful properties. The order does not matter, merging A with B equals merging B with A. Merging the same data twice changes nothing. And you can merge three phones in any grouping. That means there is no need for timestamps, no last-writer-wins, and no dialog asking which save to keep.
The same rule in two places
The merge function is not the only place the rule lives. The setters that write a star count to local storage only accept a value higher than the stored one. So even a buggy caller could not lower progress. The cloud merge and the local setters agree, which gives two independent layers of protection.
The sync step itself is short. Read the remote text, merge it with an export of local progress, apply the merged result locally if it differs, and return the merged text to be written back to the cloud. Both platforms call that one function and only differ in how they fetch and store the text.
What stays on the device
Not everything belongs in the cloud. Settings, hint counts, snowflakes, the text of a private wish capsule and any photos stay on the phone. That keeps the privacy story simple, no extra data types are sent to us, and it keeps the blob tiny. Whitelisting keys by prefix in one function makes that decision easy to audit.
Where the rule does not work
Taking the maximum only works for values that cannot legitimately decrease. A reset progress button is the classic trap: the cloud still holds the old high numbers and merges them back. If your game needs real deletion, you need tombstones or version stamps, which is a different and bigger design. We kept to values that only grow. The reset button in our game clears only the local copy, so with cloud save on, the next sync can bring the old stars back; we accept that for now.
Testing is mostly a ritual: solve a door, delete the app, install again, sign in, and watch the stars return. On iPhone expect a short delay, because iCloud sync is not instant.
FAQ
Why take the maximum instead of the newest save?
Progress values only go up, so the larger number is always correct, and it needs no timestamps or conflict dialogs.
What is stored in the cloud?
Door stars, level stars, unlocked doors and a total counter. Settings, snowflakes, hints, wish capsule text and photos stay on the device.
What happens if the cloud is unavailable?
The app keeps working and saves locally. The merge happens the next time the cloud can be reached.
Does this work for scores that can go down?
No. For values that can decrease, such as a reset or a removed item, you need version stamps or tombstones.
If you want to see this in action, Snowlings is free on Google Play and needs no account. More notes like this one live on my site.
Top comments (0)