Swift Core Data – 更改持久存储的位置

huangapple go评论68阅读模式
英文:

Swift Core Data - Change location of persistent store

问题

我有一个Swift程序,可以读取CSV文件并将数据保存到Core Data的持久存储中。我想要更改持久存储的位置。根据苹果文档,似乎可以通过在NSPersistentContainer的子类中覆盖默认目录来实现这一点。以下是我的尝试,但不成功。Xcode错误消息是“无法调用非函数类型的URL”。

final class CoreDataContainer: NSPersistentContainer {
    let storePathURL: URL = URL(string: "file:///Users/Chris/Developer/Persistent Store")!
    override class func defaultDirectoryURL() -> URL {
        return super.defaultDirectoryURL()
            .appendingPathComponent(storePathURL.lastPathComponent)
   }
}
英文:

I have a swift program that reads CSV files and saves the data to a core data persistent store. I would like to change the location of the persistent store. Per Apple documentation it appears that I should be able to do so by overriding the default directory in a sub class of NSPersistentContainer. Below is my unsuccessful attempt. The Xcode error is "cannot call value of non-function type URL".

final class CoreDataContainer: NSPersistentContainer {
    let storePathURL: URL = URL(string: "file:///Users/Chris/Developer/Persistent Store")!
    override class func defaultDirectoryURL() -> URL {
        return super.defaultDirectoryURL()
            .absoluteURL(storePathURL)
   }
}

答案1

得分: 1

以下是翻译好的部分:

let storePathURL = // ...
let description = NSPersistentStoreDescription(url: storePathURL)
let container = NSPersistentContainer(name: "YourAppName")

container.persistentStoreDescriptions = [description]

container.loadPersistentStores { _, error in
   // ...
}
英文:

Not sure if this is what you are asking:

let storePathURL = // ...
let description = NSPersistentStoreDescription(url: storePathURL)
let container = NSPersistentContainer(name: "YourAppName")

container.persistentStoreDescriptions = [description]

container.loadPersistentStores { _, error in
   // ...
}

答案2

得分: 0

谢谢你的输入。我将我的文件夹移动到了下载文件夹,并在沙盒中添加了读/写权限。以下是可工作的更新代码。

class CoreDataManager: ObservableObject {
    let persistentContainer: NSPersistentContainer
    static let shared = CoreDataManager()
    private init() {
        let storePathURL: URL = URL(fileURLWithPath: "/Users/Chris/Downloads/Persistent Store/Index Funds", isDirectory: false)
        let description = NSPersistentStoreDescription(url: storePathURL)
        persistentContainer = NSPersistentContainer(name: "Index Funds")
        persistentContainer.persistentStoreDescriptions = [description]
        persistentContainer.loadPersistentStores{ description, error in
            if let error = error {
                fatalError("Unable to initialize Core Data \(error)")
            }
        }
    } // end init
}

希望这有所帮助!

英文:

Thank you for the input. I moved my folder into Downloads and added read/write access to it in the sandbox. Below is the updated code that works.

class CoreDataManager: ObservableObject {
    let persistentContainer: NSPersistentContainer
    static let shared = CoreDataManager()
    private init() {
        let storePathURL: URL = URL(fileURLWithPath: "/Users/Chris/Downloads/Persistent Store/Index Funds", isDirectory: false)
        let description = NSPersistentStoreDescription(url: storePathURL)
        persistentContainer = NSPersistentContainer(name: "Index Funds")
        persistentContainer.persistentStoreDescriptions = [description]
        persistentContainer.loadPersistentStores{ description, error in
            if let error = error {
                fatalError("Unable to initialize Core Data \(error)")
            }
        }
    } // end init
}

huangapple
  • 本文由 发表于 2023年4月17日 04:49:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定