简体   繁体   English

核心数据如何与托管对象进行交互?

[英]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.

相关问题 如何使用Core Data验证托管对象存储的sqlite文件 - How to validate managed objects stored sqlite file with Core Data 如何在 Core Data 中为一对多托管对象使用谓词 - How to use a predicate for one-to-many managed objects in Core Data swift中的核心数据管理对象 - 是否允许? - Core Data managed objects in swift - is nil allowed? 新手,核心数据实例化托管对象 - Newbie, Core data instantiate managed objects 如何迅速将核心数据管理对象数组转换为“可识别”列表? (Xcode 11,Beta 5) - How to transform array of core data managed objects into an “identifiable” list, in swift? (Xcode 11, Beta 5) 如何从干净的托管对象上下文开始而不删除核心数据中的对象 - How do to start with clean Managed Object Context without deleting objects in Core Data 在Xcode 7.3中使用Swift实例化核心数据托管对象 - Instantiating Core Data Managed Objects Using Swift in Xcode 7.3 如何更新核心数据中的对象? - How to update objects in core data? 如何创建Core Data NSManagedObject的独立实例-不受管理 - How to create standalone instance of Core Data NSManagedObject - not managed 如何注意到核心数据处理的托管对象实体的更改? - How to notice changes on Managed Object Entity handled by Core Data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM