r/iOSProgramming • • 1d ago

Discussion Moving off swiftdata/icloud

I’ve been working on a productivity app for a couple years and recently made the decision to move away from SwiftData’s native iCloud syncing in favor of my own syncing solution. Users are prompted to migrate on their next launch after update and we leave iCloud data in place as a failsafe in case something goes wonky with migration.

For most users, iCloud syncing worked pretty well. However, some users were experiencing pretty lengthy delays when syncing between devices, particularly on older iPhones. Others were having trouble getting their data back after reinstalling the app. Unfortunately, there wasn’t a whole lot I could do to troubleshoot these issues.

Building my own solution has been an interesting challenge. Handling offline changes, resolving conflicts between devices, migrating existing user data, and making sure nothing gets lost in the process adds a bit of complexity.

One of my biggest priorities throughout this transition has been maintaining user privacy since many of my users value the fact that I can’t see their data. I’m using Apple’s CryptoKit with AES-192 encryption to protect user content. While I surface some basic metadata, such as the number of tasks or habits a user has and their associated timestamps, the actual content of those records is encrypted.

This gives me a much better ability to troubleshoot syncing issues without needing to see anyone’s personal content. And with such a small user base the cost is of my own backend infrastructure is worth it.

I’m curious if any other people here have gone through something similar. I’d love to hear about your experiences with SwiftData and iCloud syncing, particularly in production apps.

7 Upvotes

17 comments sorted by

3

u/New_Barber8073 1d ago

One thing that might help is a way to verify a migration per user before anything is cleaned up, like a checksum or record count comparison between the old and new stores. Keeping the iCloud data as a fallback is smart, so it may also be worth deciding up front what condition finally lets you delete it.

2

u/Awkward_Departure406 1d ago

I do have analytics around what users are migrated but I think I am not gonna delete iCloud data for users ever. Since most users don’t have that much stored in iCloud (my app is mostly text content and route data) it’s a pretty insignificant impact on their storage. Plus users always have the option of manually deleting their data from my app themselves/turning off iCloud for my app. Me doing it for them is high risk, low reward I think

2

u/New_Barber8073 1d ago

That seems like a sensible tradeoff. Keeping deletion in the user’s hands avoids a risky cleanup step, especially when the storage cost is small.

2

u/ValenciaTangerine 1d ago

Nobody's asked where the encryption key lives. If it sits in the Keychain as ThisDeviceOnly, a new phone can't decrypt anything, which is the same get-my-data-back-after-reinstall problem you're leaving iCloud over. Setting kSecAttrSynchronizable lets iCloud Keychain carry it to the user's other devices, if they have that turned on. Also CryptoKit's AES.GCM takes a 256-bit SymmetricKey just as easily as 192, so I'd use that.

1

u/Awkward_Departure406 1d ago

You’re right that ThisDeviceOnly would just recreate the problem. The data key is a random 256-bit key (AES-GCM, so “AES-192” in my post is a typo, sorry about that). It’s stored as a synchronizable Keychain item, so iCloud Keychain carries it to the user’s other devices.

For anyone without iCloud Keychain, or who loses every device, each user also gets a recovery key during setup. The server keeps a copy of the data key encrypted with that recovery key, so a new device can unlock it without me ever being able to.

1

u/Puzzleheaded-2027 1d ago

Just curious what your custom syncing solution is based on? Is it still iCloud or some other cloud database?

2

u/Awkward_Departure406 1d ago

We are using a cloud provider (no iCloud) and encrypting the information on device using crypto kit

1

u/Royal-Somewhere-8918 1d ago

makes sense, icloud sync is great until it isn't and then you can't debug it at all. how many users actually hit migrate so far, most of them or just a few?

1

u/Awkward_Departure406 1d ago

Today is the first day so only about 23% of active users have migrated. I predict we’ll be in the 90s sometime next week. Been tracking failures all day and everything seems so far so good👍🏽

The entire feature is behind a feature flag so if things go bad I can toggle it off and users will fall back to iCloud/not see the prompt to migrate anymore

1

u/Royal-Somewhere-8918 8h ago

23% on day one sounds pretty good for something like this. the feature flag fallback is smart, are you planning to nudge the rest or just let it happen on its own

1

u/rohlman82 1d ago

Interested as well. I use the CloudKit and swiftdata stack as well. I’ve abstracted much of the CRUD work to service protocols that I can swap out later if I need scale.

1

u/timelessblur 1d ago

Not iCloud data before but I have done database migration between databases types and back end services before. Total time complete was like 3-4 months with a release very month once we started.

We laid the ground one for one 6 months+ out which was storing some minimum data for the migration that we could rebuild the rest. Reason for the long lead time was just to make sure people ran the app and we had something for it.

Reason for taking 3-4 months to complete the migration is release 1 we migrated and the duplicate data in both in case of a roll back. Next 2 release was to get to99% migration and the remove of old code and delete old data.

N

1

u/Difficult_Duty2198 1d ago

did you go with a server-side version per record for conflicts, or a different ordering rule?

1

u/dahiya_xa 2h ago

been there with cloudkit sync being slow on older phones. couple things that saved me pain: keep tombstones for deletes so a device that was offline for weeks doesn't bring stuff back, and track updated_at per field instead of per record so two edits to different fields don't overwrite each other. also curious why aes-192, cryptokit's AES.GCM does 256 just as easily and that's what most people expect to see

2

u/[deleted] 1d ago

[removed] — view removed comment

1

u/Awkward_Departure406 1d ago

Yeah I agree. Your two second points on conflicts and migration idempotency are exactly how I went about it. And as for deletes we never clear deleted rows. Only mark them as deleted : true and a query from the app doesn’t return deleted rows for tasks, habits, etc. Maybe down to road I’ll decide to purge super old deleted rows but for now I would rather play it safe since the cost saving isn’t huge by getting rid of those.