Read about my struggles with the new NSPersistentCloudKitContainer and how I got it to work in my app Hippo.
This is going to be a bit of a technical post.
During WWDC 2019 Apple announced a super simple way of adding iCloud synchronization to your app with NSPersistentCloudKitContainer. The promise is great: simply switch your CoreData stack from a regular NSPersistentContainer to NSPersistentCloudKitContainer and voila, your app will sync 🥳.
While this basic premise does hold up, I encountered quite some practical issues while adding iCloud sync to my app Hippo. To be honest, it drove me kind of crazy, so much that I almost ditched iCloud sync to move on to something easier 😬
I thought to share some of my biggest struggles here, maybe it will help you with getting iCloud sync into your app!
Probably your users will have some data in the app after you add iCloud sync. But NSPersistentCloudKitContainer did not sync existing to iCloud from my app initially.
The issue was that the original NSPersistentContainer did not have history tracking enabled. Thus only newly created objects or updated objects would be pushed to iCloud.
To solve this I implemented an ugly work-around: I've added a isSyncedToiCloud flag to all CoreData entities which defaults to false. Then on launch, the app checks if there are entities that havent been updated and will flip this flag.
This will trigger NSPersistentCloudKitContainer to push the entities to iCloud.
This issue took me forever to debug. When you add a new entity to your model, you could get these kind of errors: Constraint unique violation, reason=constraint violation during attempted migration It turns out that this is a bug in NSPersistentCloudKitContainer on iOS 13.
New models should be ordered alphabetically and be added at the bottom of the list 🤦♂️. My solution was to prepend a "Z" to the new entity name, but this is quite an ugly solution.
This should be fixed in the latest iOS 14 beta's, but while you support iOS 13 you'll need to be careful with naming new entities.
Most apps allow users to enable and disable sync in the apps settings screen. Or sync is a premium feature only enabled if you have a subscription.
I tried to implement a setting switch to disable iCloud in Hippo from the app, but it's rather impossible to implement with NSPersistentCloudKitContainer.
First of all, there is a recommended way to turn off syncing, by setting cloudKitContainerOptions to nil. Be sure to do this before you call loadPersistentStores.
But, you will run into issues with this code. Since the container will only be loaded once when the app starts, and its contexts will be used in various views and background processes in your app, it's not simple to change this while your app is running.
I tried for quite some time to get this implemented but gave up after I got a reply from Apple. I now point my users to their iCloud settings in the Settings app to enable or disable sync. Not ideal, but I have no clue how to do this otherwise.
I've submitted Hippo 1.4 with NSPersistentCloudKitContainer to Apple today, hopefully my users can enjoy the benefits of syncing, and I can move on to another feature 😁
Ever since the web started I have been creating websites and apps. I built websites for local business in my hometown and ended up working for the largest restaurant website in Europe.
But in the recent years I switched from a technical/UX to a product management career. Being a product manager meant a lot of coordinating, planning and managing. Although that was a lot of fun and I released some great products with my team, I wasn't actually building anything myself anymore.
In the past months I had time off to reflect and look ahead. And I decided to get my hands dirty again and go back to programming and designing. I want to create stuff! I want to work on projects that I find useful and I think can be useful to others. So I'm going to build my own product. Which is exciting and super scary at the same time!
I'm starting an indie app company. The goal of this endeavor is to refresh my design and programming skills which I have neglected in the past years.
I quit my job so I can focus on this full time. I have given myself 6 months to see if I'm going to be successful at indie making. In this time I want to design, create and ship an iOS app. To earn money, the app will have some paid component, either a subscription or a premium feature set.
My aim is to have my first project in the app store by the end of March.
I trust my design, programming and product management skills but I'm less confident in my marketing abilities 😁 So I will also work on building a following and reach on different social media, blog about my journey on this site and recruit/interview possible customers.
The first app I'm creating is a note taking app with a little twist, I'll share more about this idea in a future blog post.