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

标签 ios swift core-data icloud

我有一个应用程序,它使用旧的 iCloud 同步,我想删除此功能。 因此,当我尝试从 persistenceStoreCoordinator 中的选项中删除“NSPersistentStoreUbiquitousContentNameKey”时,我将丢失整个数据库项目。有人知道我必须做什么吗?数据库迁移?

    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

最佳答案

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

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

关于ios - 如何从核心数据数据库中删除 iCloud 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48223958/

相关文章:

ios - 设置图层的 zPosition 与通过 CATransform3DTranslate 更改它有什么区别

ios - 完成 block 完成后如何调用方法?

ios - swift 函数可以有一个默认的完成处理程序吗?

ios - UIPickerView 文本比例不够

iphone - iO 上有好的缓存模式吗?

swift - entityForName : nil is not a legal NSPersistentStoreCoordinator for searching for entity name

ios - 使用 mDNS 发现 iOS 设备名称

ios - 在 UITextVIew 编辑为真时检测 NSAttributedString 中附加图像的点击

iphone - 如何将核心数据添加到选项卡栏应用程序

ios - 如何使用 swift 在 ios 中访问 Cookie 属性,即名称、版本、值等?