How to sync data between your app running on multiple devices?

Mark van Wijnen
5 min readApr 16, 2023
Photo by Firmbee.com on Unsplash

Introduction

So the idea is to store the data of the app using Core Data and use CloudKit to store this data in the cloud and sync it between the devices.

In this story I will walkthrough the easiest and minimum amount of steps to get this up and running. What you will end up with is an app that is able to create and delete records and you will see it update on all your devices.

If you came here just for the solution you can skip the backstory and go straight to the setup.

Backstory

Let me take you through the process I went through when I first attempted to create this and why I felt I needed to write this story.

If you are like me, you quickly want to see if the tools provided by Apple work. You want to have the app open on the Mac and iPhone and see the data sync between those two platforms with your own eyes. This way you know you have a good foundation to build the rest of your app upon.

So you open Xcode and create a new multiplatform project. Here you will notice a few interesting checkboxes: Use Core Data and Host in CloudKit.

My assumptions were that with both checked Apple will give me a base template that will work out of the box. That I will get Core Data that has its container live in the cloud and with my app reading and writing to it, it will automatically sync across all my devices.

My assumptions were wrong. When I ran the app on Mac and iPhone I could add and delete records but it stored it locally. So a record created on Mac did not show up on the iPhone and vice versa. The data was stored across launches of the app, but not synced across devices. It left me confused and it might also be the reason why you ended up here.

There are actually a few more ‘hidden’ steps we need to perform to make it work the way we imagined it. Those steps will be mentioned in the setup section below.

Setup

Create a New Project

Open Xcode and choose Multiplatform App as the template for your project.

--

--

Mark van Wijnen

macOS/iPadOS/iOS/watchOS/visionOS developer and SwiftUI enthousiast. “Stay Hungry, Stay Foolish!” — Steve Jobs