Search code examples
iosswiftcore-dataicloud

Save CoreData to iCloud (Swift)


I have already a CoreData, then now i want to save a data to iCloud.

In AppDelegate.swift

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
        var coordinator: NSPersistentStoreCoordinator =
        NSPersistentStoreCoordinator (managedObjectModel: self.managedObjectModel)

        var storeURL =
        self.applicationDocumentsDirectory
            .URLByAppendingPathComponent("DeviceCoreData.sqlite")

        var storeOptions =
        [NSPersistentStoreUbiquitousContentNameKey : "MyDevices"
            // NSPersistentStoreRebuildFromUbiquitousContentOption: @(true)
        ]

        //
        self.registerCoordinatorForStoreNotifications (coordinator)

        var error : NSError? = nil
        var store : NSPersistentStore! =
        coordinator.addPersistentStoreWithType (NSSQLiteStoreType,
            configuration: nil,
            URL: storeURL,
            options: storeOptions,
            error: &error)

        if nil == store {
            // handle error
        }

        return coordinator
        }()

func registerCoordinatorForStoreNotifications (coordinator : NSPersistentStoreCoordinator) {
    let nc : NSNotificationCenter = NSNotificationCenter.defaultCenter();

    nc.addObserver(self, selector: "handleStoresWillChange:",
        name: NSPersistentStoreCoordinatorStoresWillChangeNotification,
        object: coordinator)

    nc.addObserver(self, selector: "handleStoresDidChange:",
        name: NSPersistentStoreCoordinatorStoresDidChangeNotification,
        object: coordinator)

    nc.addObserver(self, selector: "handleStoresWillRemove:",
        name: NSPersistentStoreCoordinatorWillRemoveStoreNotification,
        object: coordinator)

    nc.addObserver(self, selector: "handleStoreChangedUbiquitousContent:",
        name: NSPersistentStoreDidImportUbiquitousContentChangesNotification,
        object: coordinator)
}

But when i build application, i got a error message

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Ơn_Giời.AppDelegate handleStoresDidChange:]: unrecognized selector sent to instance 0x17668770'

Someone can help me ? I stack here 3 days.

And

[NSPersistentStoreUbiquitousContentNameKey : "MyDevices"]

This Key is correct or not ? My cloudkit named iCloud.MyDevices Thanks for your help...


Solution

  • This line:

    nc.addObserver(self, selector: "handleStoresDidChange:",
        name: NSPersistentStoreCoordinatorStoresDidChangeNotification,
        object: coordinator)
    

    says that when that notification is posted, a method named handleStoresDidChange: should be called, on the object self (apparently, the app delegate)

    The error:

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Ơn_Giời.AppDelegate handleStoresDidChange:]: unrecognized selector sent to instance 0x17668770'
    

    says that AppDelegate does not have a method named handleStoresDidChange:.

    This is why the app crashes: You have instructed the notification center to call a method that does not exist. If you tell the notification center it should call a method, that method must exist. You need to either create that method or tell the notification center to call some other method that does exist.