[英]How Core Data interacts with Managed Objects?
Here is my test code and I can't really understand why print("db2: \\(prod)")
writes that my element has attribute belok = 10000
. 这是我的测试代码,我无法真正理解为什么
print("db2: \\(prod)")
写道我的元素具有belok = 10000
属性。 But I didn't save the context, so in DB there is no changes, it's ok. 但是我没有保存上下文,因此在数据库中没有更改,没关系。 I'm trying to understand how interact DB and my array
arrDBProducts
. 我试图了解如何与数据库和数组
arrDBProducts
。
var arrDBProducts: [DB_Product]
func test() {
let fetchRequest: NSFetchRequest<DB_Product> = DB_Product.fetchRequest()
do {
let prods = try CoreDB.context.fetch(fetchRequest)
for prod in prods {
print("db1: \(prod)")
}
}
catch {}
arrDBProducts[0].belok = 10000
print("array: \(arrDBProducts)")
let fetchRequest2: NSFetchRequest<DB_Product> = DB_Product.fetchRequest()
do {
let prods = try CoreDB.context.fetch(fetchRequest2)
for prod in prods {
print("db2: \(prod)")
}
}
catch {}
}
You are didn't save the context, so once you will close and open your app or if you will try to access it from another context - you will not see that changes. 您没有保存上下文,因此一旦关闭并打开您的应用程序,或者尝试从其他上下文访问它-您将看不到更改。
But, while you are working in one context - your changes will exist in it until context will be released. 但是,当您在一个上下文中工作时-您的更改将一直存在于其中,直到上下文被释放为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.