如何从核心数据数据库中删除iCloud同步

时间:2018-01-12 10:12:20

标签: ios swift core-data icloud

我有一个应用,它正在使用旧的iCloud同步,我想要删除此功能。 因此,当我尝试删除' NSPersistentStoreUbiquitousContentNameKey'从persistentStoreCoordinator中的选项,我将丢失我的整个数据库项。有人知道我要做什么吗?数据库的迁移?

    let containerPath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.de.companyName.appname")?.path
    let sqlitePath = NSString(format: "%@/%@", containerPath!, "AppName")
    let url = URL(fileURLWithPath: sqlitePath as String)

    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    var error: NSError? = nil

    var failureReason = "There was an error creating or loading the application's saved data."
    let mOptions = [NSPersistentStoreUbiquitousContentNameKey: "AppNameCloud", NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true] as [String : Any]
    do {
        try coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: mOptions)
    } catch var error1 as NSError {
        error = error1
        coordinator = nil
        // Report any error we got.
        var dict = [String: AnyObject]()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
        dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
        dict[NSUnderlyingErrorKey] = error
        error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)

        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    } catch {
        fatalError()
    }

    return coordinator

1 个答案:

答案 0 :(得分:0)

迁移就是我要做的。删除无处不在的内容密钥使Core Data加载不同的持久存储。由于它是新的和空的,因此您的现有数据不存在。

您要做的是加载现有的iCloud数据存储,然后使用migratePersistentStore(_:to:options:withType:)上的NSPersistentStoreCoordinator方法将所有数据移动到非iCloud存储。完成后,开始使用新的商店文件。