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

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

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