Core data Mac Catalyst CoreData在iOS而不是macOS上运行

Core data Mac Catalyst CoreData在iOS而不是macOS上运行,core-data,mac-catalyst,Core Data,Mac Catalyst,我尝试在Mac Catalyst上运行iOS应用程序,但我的应用程序有核心数据。我读过这篇文章: 但我并没有实现这一目标。我不知道 我在视图控制器中编写了这段代码,其中使用了核心数据。它与AppDelegate.swift文件中的代码相同(我没有删除它) 当我在核心数据中更新或插入数据时: // SAVE the context. do { try context.save() } catch { let nserror = error as NSError print

我尝试在Mac Catalyst上运行iOS应用程序,但我的应用程序有核心数据。我读过这篇文章:

但我并没有实现这一目标。我不知道

我在视图控制器中编写了这段代码,其中使用了核心数据。它与AppDelegate.swift文件中的代码相同(我没有删除它)

当我在核心数据中更新或插入数据时:

// SAVE the context.
do {
    try context.save()
} catch {
    let nserror = error as NSError
    print("Unresolved error \(nserror), \(nserror.userInfo)")
}

#if targetEnvironment(macCatalyst)
// SAVE the context.
self.saveContext()
#endif
我很迷路。我需要知道如何编写代码。我知道这并不复杂,但我迷路了

我展示了我用来读/写核心数据的代码

let appDelegate = UIApplication.shared.delegate as! AppDelegate

let context = appDelegate.persistentContainer.viewContext

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Topic")

fetchRequest.returnsObjectsAsFaults = false

let results = try? context.fetch(fetchRequest)

let newSelectedValue = string

if (results?.count)! > 0 {
    for updateItem in results as! [NSManagedObject] {
        updateItem.setValue(newSelectedValue, forKey: "topicName")
    }
} else {
    let newItem = Topic(context: context)
    newItem.topicName = newSelectedValue
}

// SAVE the context.
do {
    try context.save()
} catch {
    let nserror = error as NSError
    print("Unresolved error \(nserror), \(nserror.userInfo)")
}
让appDelegate=UIApplication.shared.delegate为!AppDelegate
让上下文=appDelegate.persistentContainer.viewContext
let fetchRequest=NSFetchRequest(entityName:“主题”)
fetchRequest.returnsObjectsAsFaults=false
让结果=尝试?context.fetch(fetchRequest)
让newSelectedValue=string
如果(结果?.count)!>0 {
对于结果中的updateItem![NSManagedObject]{
updateItem.setValue(newSelectedValue,forKey:“topicName”)
}
}否则{
让newItem=Topic(上下文:context)
newItem.topicName=newSelectedValue
}
//保存上下文。
做{
尝试context.save()
}抓住{
设nserror=错误为nserror
打印(“未解决的错误\(nserror),\(nserror.userInfo)”)
}

据我所知,你读的帖子让你困惑,而不是帮助你。它建议将核心数据代码从AppDelegate移动到主ViewController,这是我个人不同意的

使用Catalyst时,您可以对核心数据使用完全相同的代码

let appDelegate = UIApplication.shared.delegate as! AppDelegate

let context = appDelegate.persistentContainer.viewContext

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Topic")

fetchRequest.returnsObjectsAsFaults = false

let results = try? context.fetch(fetchRequest)

let newSelectedValue = string

if (results?.count)! > 0 {
    for updateItem in results as! [NSManagedObject] {
        updateItem.setValue(newSelectedValue, forKey: "topicName")
    }
} else {
    let newItem = Topic(context: context)
    newItem.topicName = newSelectedValue
}

// SAVE the context.
do {
    try context.save()
} catch {
    let nserror = error as NSError
    print("Unresolved error \(nserror), \(nserror.userInfo)")
}

因此,我建议您删除专门为macCatalyst编写的代码,并在同一位置加载iOS和macOS的PersistentStores。

我部分理解它的意思。我也不认为将核心数据方法从AppDelegate.swift移动到另一个视图控制器是非常正确的:在我看来,这是一个“糟糕”的解决方案,但我迷失在你的短语中:“加载PersistentStores”我不太明白(这是我的错),我修改了这个问题以附加我用来编写/读取核心数据的代码。