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

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)")


Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.