fatbobman / blogComments

1 stars 0 forks source link

CoreData 探秘 - 从数据模型构建到托管对象实例 | 肘子的Swift记事本 #200

Open fatbobman opened 11 months ago

fatbobman commented 11 months ago

https://www.fatbobman.com/posts/from-Data-Model-Construction-to-Managed-Object-Instances-in-Core-Data/?

对每一个使用 Core Data 的开发者来说,用 Xcode 的 Core Data 模型编辑器构建数据模型、创建容器、加载数据模型并通过托管对象上下文最终创建托管对象实例,这都是十分普通的过程。但你是否好奇过这一切的内部运行机制,Core Data 是如何在幕后辅助我们完成这一切的?本文将深入探究 Core Data 是如何通过数据模型构建出托管对象实例的内部运行机制,读完本文可以让你对 Core Data 的工作流程有更深入的理解,在开发中可以更得心应手。

mcmay commented 8 months ago

Thanks for sharing this great discussion about Core Data's way of working with data model creation and management. After reviewing this process, I started to understand why the newly introduced SwiftData is said to be using a quite different paradigm of modeling and manipulating data despite the fact that it is still built on top of Core Data. However, on the surface at least, SwiftData adopts the declarative programming paradigm in an attempt to hide away all the granular Core Data operations going on under the hood, as described in your article. For example. it separates the creation of the model container to the @main part of the project and then in individual views and files it uses the @Model macro to model the data which is discussed in-depth in another of your articles and the @Environment(\modelContext) as well as @Query macros to do the CRUD operations to ensure the changes to fetched data are saved automatically if you enable that option. Anyway, given SwiftData is still in its budding stage, it would be all the same necessary to learn how Core Data works as Core Data will still live for a long time to come in existing and emerging projects and a great proportion of the crash information or error messages for apps using SwiftData do include Core Data idioms such as NSManagedObjectModel.

fatbobman commented 8 months ago

Thanks for sharing this great discussion about Core Data's way of working with data model creation and management. After reviewing this process, I started to understand why the newly introduced SwiftData is said to be using a quite different paradigm of modeling and manipulating data despite the fact that it is still built on top of Core Data. However, on the surface at least, SwiftData adopts the declarative programming paradigm in an attempt to hide away all the granular Core Data operations going on under the hood, as described in your article. For example. it separates the creation of the model container to the @main part of the project and then in individual views and files it uses the @model macro to model the data which is discussed in-depth in another of your articles and the @Environment(\modelContext) as well as @query macros to do the CRUD operations to ensure the changes to fetched data are saved automatically if you enable that option. Anyway, given SwiftData is still in its budding stage, it would be all the same necessary to learn how Core Data works as Core Data will still live for a long time to come in existing and emerging projects and a great proportion of the crash information or error messages for apps using SwiftData do include Core Data idioms such as NSManagedObjectModel.

The direction of SwiftData is very promising, but unfortunately, the current implementation seems a bit rushed, and it's estimated that it will take one or two major version updates before it can come close to the capabilities of Core Data. This year, I plan to gradually write some articles related to the specific functionalities of SwiftData. However, I expect to write one or two corresponding Core Data articles before each of those, to provide a more detailed explanation. Just today, I published a new article that introduces relationships in Core Data.

https://fatbobman.com/en/posts/mastering-relationships-in-core-data-fundamentals/

mcmay commented 8 months ago

Thanks for your response. That's true. SwiftData in its current beta version certainly has a lot of space for improvement and expansion to match Core Data's maturity. For example, SwiftData's undo and redo capabilities are still in an unstable state so developer often have to implement those functionalities by writing their own code.

Thanks for providing the great opportunity to learn about relationship management in Core Data. I will read through it soon.