简体   繁体   English

想要保存核心数据,但“扩展不得包含存储的属性”

[英]Want to save Core Data but “Extensions must not contain stored properties”

I have a Core Data class called "CloudPage", and I've added an extension for it in my code:我有一个名为“CloudPage”的核心数据 class,并在我的代码中添加了一个扩展:

extension CloudPage:GalleryItem {
    // bunch of methods defined here
    func someFunc() -> Bool {
        return true
    }

It made sense to me to extend this class to have all model information reside here.扩展此 class 以使所有 model 信息都驻留在此处对我来说是有意义的。 I also thought it made sense for this class to know how to save an instance of itself, so I added my viewcontext to it:我还认为这个 class 知道如何保存自己的实例是有意义的,所以我将我的 viewcontext 添加到它:

extension CloudPage:GalleryItem {
    @Environment(\.managedObjectContext) private var viewContext

// ...some code...

But now swift says "Extensions must not contain stored properties".但是现在 swift 说“扩展不得包含存储的属性”。 Is there a better way to do this?有一个更好的方法吗? I would think all saving logic should reside on this model.我认为所有保存逻辑都应该驻留在这个 model 上。

As the compiler already say, it is not allow to add stored properties in Extension.正如编译器已经说过的那样,不允许在 Extension 中添加存储的属性。 CloudPage is an NSManagedObject which means it is already managed by CoreData. CloudPage是一个NSManagedObject ,这意味着它已经由 CoreData 管理。

Anyway this is not how you work with CoreData Objects.无论如何,这不是您使用 CoreData 对象的方式。 I'll clarify some stuff我会澄清一些事情

These objects are managed/loaded in a context.这些对象在上下文中管理/加载。 There are 2 types of contexts有两种类型的上下文

  1. ViewContext (should be used for view purposes) ViewContext(应该用于查看目的)
  2. BackgroundContext (should be used for loading stuff from eg APIs in the background) BackgroundContext(应该用于在后台从例如 API 加载内容)

If you update objects or add new ones, these already live in said context.如果您更新对象或添加新对象,这些对象已经存在于所述上下文中。 To persist these objects you don't have to "push" them in there anymore like you'd do it with plain SQL ( UPDATE table... ).要持久保存这些对象,您不必再像使用普通 SQL ( UPDATE table... )那样将它们“推”入其中。 You simply have to save the context like您只需保存上下文,例如

// context is the one you loaded the objects from and worked with
context.save()

I hope this helps, i just wanted to sum it up roughly for you to get a better understanding.我希望这会有所帮助,我只是想粗略地总结一下,以便您更好地理解。 Don't become desperate, CoreData has its quirks and you'll get used to it.不要绝望,CoreData 有它的怪癖,你会习惯的。 For more details read Apples Documentation of working with Core Data .有关更多详细信息,请阅读Apple 使用 Core Data 的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 扩展 class 中的 Var 显示错误“扩展不得包含存储的属性” - Var in extension class show error “Extensions must not contain stored properties” 扩展名可能不包含存储的属性 - Extensions May not contain Stored properties Alamofire-将数据保存在存储的属性中 - Alamofire - save data in stored properties 具有存储属性的扩展 - Extensions with stored properties 我想保存到CORE DATA,但是我一直在获取“ Reminder”类的NSManagedObject必须具有有效的NSEntityDescription。 - I want to save to CORE DATA, but I keep getting a 'An NSManagedObject of class 'Reminder' must have a valid NSEntityDescription.' Objective C&Swift互操作性导致错误“扩展可能不包含存储的属性” - Objective C & Swift Interoperability causes error “Extensions may not contain stored properties” 保存包含相同核心数据对象的多个阵列 - Save multiple Arrays that contain the same Core Data Object 如果数据已存在于核心数据中,则不想保存数据 - Don't want to save data if it already exists in core data 将实体属性数据保存到核心数据后如何保留 - How to keep entity properties data after save it on core data 我想在核心数据中保存 UISegmentedControl.selectedSegmentIndex - 但是如何 - I want to save UISegmentedControl.selectedSegmentIndex in Core Data - but how
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM