简体   繁体   English

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

[英]How can I remove iCloud sync from core data database

I have an app, which is using the old iCloud sync and I want to remove this feature. 我有一个应用程序,该应用程序正在使用旧的iCloud同步,并且我想删除此功能。 So when I try to remove the 'NSPersistentStoreUbiquitousContentNameKey' from options in persistentStoreCoordinator, I will lose my whole database items. 因此,当我尝试从persistentStoreCoordinator中的选项中删除“ NSPersistentStoreUbiquitousContentNameKey”时,我将丢失整个数据库项。 Do anybody know what I have to do? 有人知道我该怎么做吗? Migration of database? 迁移数据库?

    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

Migration is what I'd do. 迁移是我要做的。 Removing the ubiquitous content key makes Core Data load a different persistent store. 删除无处不在的内容密钥会使Core Data加载不同的持久性存储。 Since it's new and empty, your existing data isn't there. 由于它是新的并且是空的,因此您现有的数据不存在。

What you'd do is load the existing iCloud data store, then use the migratePersistentStore(_:to:options:withType:) method on NSPersistentStoreCoordinator to move all of the data to a non-iCloud store. 什么你做的是加载现有的iCloud数据存储,然后使用migratePersistentStore(_:to:options:withType:)对方法NSPersistentStoreCoordinator到所有的数据移动到非的iCloud存储。 When that finishes, start using the new store file. 完成后,开始使用新的存储文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM