最佳练习处理多个VC中的NsmanagedObjects和Coredata

发布于 2025-01-24 21:45:35 字数 2378 浏览 1 评论 0原文

我可以在这方面使用您的建议,

我重组了整个应用程序,以使用Coredata,这样我就可以保存我的应用程序的主要类:“ Foobar” 我还将完整的业务逻辑转移到了NsmanageBject子类Foobar() 这就是为什么我需要使用某种便利性初始化,启动自己的方法来计算价值等 这是我的课程定义:

Import Foundation
import CoreData
import UIKit

@objc(FooBar)
public class FooBar: NSManagedObject {
    
    var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext // used for other CD interactions inside this class
    var member1 = Int()
    var member2 = Double()
    var member3:Double = 0
    var fooPack:[FooPacks] = []
    ...
    
    
    convenience init(   param1: Int16
                     ,  param2: String?
                     ,  param3: Float
                     ,  param4: String?
                     ,  param5: Float
                     ,  fooPack: NSSet
                     ,  entity: NSEntityDescription
                     ,  context: NSManagedObjectContext?
                    ) {
        self.init(entity: entity, insertInto: context)
    
        self.param1 = param1
        self.param2 = param2
        self.param3 = param3
        self.param4 = param4
        self.param5 = param5
        self.addToUsedfooBarPacks(fooBarPack)
        
        self.build()
    }
    
    func build() {
        // i do something
    }
    
    func method1() {
        // i do something
    }

当我在一个viewController中初始化我的foobar类时,我会做到这一点:

self.fooBar = FooBar.init(param1: var1,
                              param2: var2,
                              iparam3: var3,
                              param4: var4,
                              param5: var5,
                              fooPack: var6,
                              entity: FooEntity,
                              context: context)

当我修改此类的任何实例时,我都会做到这一点:

self.fooBar.member2 = newValue
self.fooBar.build()

然后在特定点(在多个VC中),然后打电话给我来保存该类别实例到Coredata:

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
        try context.save()
    }catch{
        print("\(error)")
    }

使用这种方法,我经常必须处理错误和意外行为,例如: Do Block成功执行,但是该对象尚未保存!?

我经常将Foobar实例交给其他视图控制器,然后尝试将它们保存在那里, 但这似乎不是我认为更新以前获取的对象的正确方法

,这与我处理上下文变量的方式有关。 这就是为什么我要征求您的建议。.

处理这种情况的最佳方法是什么? 在过去的几个月中,我已经学到了很多关于Coredata的新知识,但是我似乎在这里错过了一些重要的东西。.

请帮助我:)

i'd could use your advice on this one

i restructured my whole app, to use coreData so i'd be able to save the Main Class of my app: "FooBar"
i also transfered my complete business logic into that NSManagedObject subclass FooBar()
that's why i needed to to use some kind of convenience init, to start my own methods for calculating values and so on
here's my class definition:

Import Foundation
import CoreData
import UIKit

@objc(FooBar)
public class FooBar: NSManagedObject {
    
    var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext // used for other CD interactions inside this class
    var member1 = Int()
    var member2 = Double()
    var member3:Double = 0
    var fooPack:[FooPacks] = []
    ...
    
    
    convenience init(   param1: Int16
                     ,  param2: String?
                     ,  param3: Float
                     ,  param4: String?
                     ,  param5: Float
                     ,  fooPack: NSSet
                     ,  entity: NSEntityDescription
                     ,  context: NSManagedObjectContext?
                    ) {
        self.init(entity: entity, insertInto: context)
    
        self.param1 = param1
        self.param2 = param2
        self.param3 = param3
        self.param4 = param4
        self.param5 = param5
        self.addToUsedfooBarPacks(fooBarPack)
        
        self.build()
    }
    
    func build() {
        // i do something
    }
    
    func method1() {
        // i do something
    }

when i initialize my FooBar class inside a viewController i do this by:

self.fooBar = FooBar.init(param1: var1,
                              param2: var2,
                              iparam3: var3,
                              param4: var4,
                              param5: var5,
                              fooPack: var6,
                              entity: FooEntity,
                              context: context)

when ever i modify any instance of this class i do this by:

self.fooBar.member2 = newValue
self.fooBar.build()

and later at a specific point (in multiple VC's) i then call to save that Instance to CoreData:

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
        try context.save()
    }catch{
        print("\(error)")
    }

with this approach i often have to deal with bugs and unexpected behaviours like:
the do block gets executed successfully, but the object has NOT been saved!?

i often hand over FooBar instances to a different ViewController and i try to save them there,
but this does not seem to be the right way to do update previous fetched objects

in my opinion, this has to do with the way i handle the context variables..
and exactly this is why i am reaching out for your advice..

What is the optimal way to handle such a situation, where i want to modify and save instances within multiple different ViewControllers?
i already learned a lot new stuff about CoreData in the last months, but i seem to miss something important here..

please help me out guys :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2025-01-31 21:45:35

您正在使用PersistentContainer,它可以帮助您设置Coredata堆栈。 persistentContainer有两种类型的上下文:viewContext背景上下文

在此设置中,我们始终使用viewContext将数据fetch Data fetch data fetch to在UI上显示。
并使用背景上下文进行更改,更新存储...

  1. 您在init方法中传递的上下文是什么?
  2. 您应该在您用来进行更改的上下文上执行context.save()(您传递给init)。在这种情况下,您不应将viewContext传递给init,并使用viewContext来调用save> save> save()
  3. 如何更新并在多个ViewControllers之间进行同步更改?
    进行更改的视图控制器也应调用保存。您可以使用委托模式将更改/广播更改为其他视图控制器...

You're using persistentContainer, it helps you setup coredata stack. persistentContainer has two types of context: viewContext and background contexts

With this setup we always use viewContext to fetch data to display on UI.
And use a background context to make changes, update storage...

  1. What is the kind of context you passed in init method?
  2. You should execute context.save() on exactly the context that you used to make changes (you passed to init). In this case you shouldn't pass viewContext to init and use viewContext to call save()
  3. How to update and sync changes between multiple ViewControllers?
    The view controller that makes changes should call save as well. And you can use delegation pattern to delegate/broadcast changes to other view controllers...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文