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

2020-07-29 00:16发布

问题:

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