最佳练习处理多个VC中的NsmanagedObjects和Coredata
我可以在这方面使用您的建议,
我重组了整个应用程序,以使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
PersistentContainer
,它可以帮助您设置Coredata堆栈。persistentContainer
有两种类型的上下文:viewContext
和背景上下文
在此设置中,我们始终使用
viewContext
将数据fetch Data fetch data fetch to在UI上显示。并使用
背景上下文
进行更改,更新存储...init
方法中传递的上下文是什么?context.save()
(您传递给init
)。在这种情况下,您不应将viewContext
传递给init
,并使用viewContext
来调用save> save> save()
进行更改的视图控制器也应调用保存。您可以使用
委托
模式将更改/广播更改为其他视图控制器...You're using
persistentContainer
, it helps you setup coredata stack.persistentContainer
has two types of context:viewContext
andbackground 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...init
method?context.save()
on exactly the context that you used to make changes (you passed toinit
). In this case you shouldn't passviewContext
toinit
and useviewContext
to callsave()
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...