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

sync issues after delete icloud data from the Settings -> iCloud -> Manage Account Storage

Forums > iOS

I applied CloudKit to CoreData in SwiftUI and found that the real-time data works well between the simulator and the real device.

Deleting iCloud data from the simulator or real device Settings -> iCloud -> Manage Account Storage causes two issues.

  1. if the app is running when you clear the icloud data in settings, the app still has the data, and when I exit the app and run it again, the data disappears.

  2. after clearing icloud data in settings, there is no real-time data synchronization between the simulator and the real device - I must go to background mode and come back to the app for it to be reflected. -> Delete and reinstall the app on the simulator and real device to get real-time data integration again.

If anyone has experienced this issue, I would appreciate your help. Thank you.

import CoreData
import Foundation

final class DataController: ObservableObject {
    let cloudKitContainer: NSPersistentCloudKitContainer

    init() {
        cloudKitContainer = NSPersistentCloudKitContainer(name: "Item")
        guard let description = cloudKitContainer.persistentStoreDescriptions.first else {
            fatalError("Failed to init persistent container")
        }
        description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)

        cloudKitContainer.viewContext.automaticallyMergesChangesFromParent = true
        cloudKitContainer.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump

        cloudKitContainer.loadPersistentStores { description, error in
            if let error {
                print("Faild Load CoreData. \(error.localizedDescription)")
                return
            }
        }
    }
}

3      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

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.