BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

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 - https://www.hackingwithswift.com/forums/swiftui/core-data-and-lifecycle-swiftui-app/2351/2404 - 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 {
            ViewRecipientsView().accentColor(.green)
                .environment(\.managedObjectContext, context)
        }
        .commands {
            CommandGroup(replacing: .help) {
                EmptyView()
            }
        }
        .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")
                saveContext()
            case .background:
                print("\(#function) REPORTS - App change of scenePhase to BACKGROUND")
                saveContext()
            @unknown default:
                fatalError("\(#function) REPORTS - fatal error in switch statement for .onChange modifier")
            }
        }
    }

        func saveContext() {
        if context.hasChanges {
            do {
                try context.save()
            } 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 RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

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

Reply to this topic…

You need to create an account or log in to reply.

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.