gpt4 book ai didi

swift - 如何与Widget共享核心数据?

转载 作者:行者123 更新时间:2023-12-02 01:36:00 24 4
gpt4 key购买 nike

我想与 WidgetKit 共享核心数据。我想我需要创建应用程序组(我知道如何做到这一点)。但是我如何与 WidgetKit 共享核心数据数据库呢?

如果我想在我的应用程序中使用 Core Data,我总是只需创建一个新的 Xcode 项目并选中“使用 Core Data”,然后我会得到此代码(Persistence.swift - 下面),这将我连接到数据库。

如何修改它,以便与 Widget 共享此 Core Data 数据库?

持久性.swift

import CoreData

struct PersistenceController {
static let shared = PersistenceController()

static var preview: PersistenceController = {
let result = PersistenceController(inMemory: true)
let viewContext = result.container.viewContext
for _ in 0..<10 {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
}
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() 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.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
return result
}()

let container: NSPersistentContainer

init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "MyAppName")
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() 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.

/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
}
}

最佳答案

PersistenceController类中首先添加一个属性来获取App Group的URL

var containerURL: URL {
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.myappname")!
}

然后在持久化容器的store描述中设置URL

init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "MyAppName")
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
} else {
let storeURL = containerURL.appendingPathComponent("MyAppName.sqlite")
container.persistentStoreDescriptions.first!.url = storeURL
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in ...

将字符串文字替换为您的实际数据。

关于swift - 如何与Widget共享核心数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72390063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com