UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 - 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.

2      

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.

2      

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?

2      

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.

2      

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.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.