TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: CloudKit Data - Syncing Inconsistently

Forums > SwiftUI

I am working on an app that syncs data to my iPhone, iPad, and Mac. I've been running it in TestFlight for some time, but it seems that the data is not syncing consistently. I've been using a lot of great information from this thread - - and for a while it seemed to be syncing fine.

What is the most effective approach for debugging syncing issues? I believe I am ready to release my app into the wild, but if the syncing doesn't work consistently that's a big show stopper for me.


I do have the following code in my @main

    @Environment(\.scenePhase) private var scenePhase
    let context = PersistentCloudKitContainer.persistentContainer.viewContext

    var body: some Scene {
        WindowGroup {
                .environment(\.managedObjectContext, context)
        .commands {
            CommandGroup(replacing: .help) {
        .onChange(of: scenePhase) { phase in
            switch phase {
            case .active:
                print("\(#function) REPORTS - App change of scenePhase to ACTIVE")
            case .inactive:
                print("\(#function) REPORTS - App change of scenePhase to INACTIVE")
            case .background:
                print("\(#function) REPORTS - App change of scenePhase to BACKGROUND")
            @unknown default:
                fatalError("\(#function) REPORTS - fatal error in switch statement for .onChange modifier")

        func saveContext() {
        if context.hasChanges {
            do {
            } catch {
                let nserror = error as NSError
                logger.log("unresolved errorL \(nserror), \(nserror.userInfo)")
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")

The goal here was to force background refresh to cloudkit whenever the app changes state.


OK.. I think I just wasn't patient enough.. I loaded my app on all my devices and after a few hours.. they were all in sync. Is there anyway to speed this up? Force a sync?


I had the cloudkit thing syncing fine on 4 devices for like a week. Got a new iPad Mini and all of the suddent, it quit working on any of them. I did nothing except change the info page and replace the code on TestFlight. installed on all devices from there (instead of building directly from xcode). Now none are working, and it's been a couple of days. Not sure if it was the info change, or adding a new build into testflight. I am ready to move to production, and would like to have this working.

Anyone got any ideas. Not sure how to debug since no change other than adding a device.


If you want to use TestFlight you have to deploy the scheme within CloudKit Control Center in your developer portal to production. Otherwise, it doesn't work. Learned it myself the hard way :) The same goes for changes in your data model and you wonder why syncing the new data fields doesn't work. You then have to deploy the changes to the scheme to production as well.


Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.