How to Migrate Core Data's Data to App Group's Data

筅森魡賤 提交于 2020-06-11 11:47:53

问题


I just added App Groups to my app using this StackOverFlow post. Unfortunately since the defaultDirectoryURL is changing, I can not fetch any of the old data I made before using the App Groups directory. I know the data is still there because when I switch back to using a regular NSPersistentContainer instead of the GroupedPersistentContainer, I can get the data.

How can I migrate my old data over to where I'm fetching the app group's data?

Core Data code:

class CoreDataManager {
    static let sharedManager = CoreDataManager()
    private init() {}

    lazy var persistentContainer: NSPersistentContainer = {
        var useCloudSync = UserDefaults.standard.bool(forKey: "useCloudSync")

        let containerToUse: NSPersistentContainer?
        if useCloudSync {
           containerToUse = NSPersistentCloudKitContainer(name: "App")
        } else {
            containerToUse = GroupedPersistentContainer(name: "App")
            let description = containerToUse!.persistentStoreDescriptions.first
            description?.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
      }

        guard let container = containerToUse, let description = container.persistentStoreDescriptions.first else {
            fatalError("Hey Listen! ###\(#function): Failed to retrieve a persistent store description.")
        }
        description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)

        container.loadPersistentStores(completionHandler: { (storeDescription, error) in

      ...

      return container
   }
}

GroupedPersistentContainer code:

class GroupedPersistentContainer: NSPersistentContainer {
    enum URLStrings: String {
        case group = "group.App"
    }

    override class func defaultDirectoryURL() -> URL {
        let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: URLStrings.group.rawValue)

        if !FileManager.default.fileExists(atPath: url!.path) {
            try? FileManager.default.createDirectory(at: url!, withIntermediateDirectories: true, attributes: nil)
        }
        return url!
    }
}

I haven't done this yet for my NSPersistentCloudKitContainer yet but it will follow the same format as this one.


回答1:


import UIKit
import CoreData

class GroupedPersistentContainer: NSPersistentContainer {

    override open class func defaultDirectoryURL() -> URL {
        var storeURL = FileManager.default
            .containerURL(forSecurityApplicationGroupIdentifier: "group.YourApp")
            .unsafelyUnwrapped

        storeURL = storeURL.appendingPathComponent("YourApp.sqlite")
        return storeURL
    }
}

See also: Sharing data using Core Data: iOS App and Extension



来源:https://stackoverflow.com/questions/61846766/how-to-migrate-core-datas-data-to-app-groups-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!