NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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 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 October 1st.

Click to save your free spot now

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.