Core Data 可以让我以编程方式创建新表吗?或者如果我需要,我需要直接使用 SQLite。
谢谢
最佳答案
从 CoreData 的角度来看,您实际上并没有创建新表,因为数据库表只是与核心数据模型关联的一种可能的持久性存储类型。
但是,您可以使用 NSEntityDescription 类以编程方式创建新的核心数据实体。在 NSEntityDescription 类文档中,您会发现:
Entity descriptions are editable until they are used by an object graph manager. This
allows you to create or modify them dynamically. However, once a description is used
(when the managed object model to which it belongs is associated with a persistent store
coordinator), it must not (indeed cannot) be changed. This is enforced at runtime: any
attempt to mutate a model or any of its sub-objects after the model is associated with a
persistent store coordinator causes an exception to be thrown. If you need to modify a
model that is in use, create a copy, modify the copy, and then discard the objects with
the old model.
我从来没有尝试过在运行时修改一个,所以我不确定当你有一个现有的 SQLite 持久性存储时它到底有多好,如果有的话。但可能值得尝试一下 NSEntityDescription,看看它是否能让您接近您想要做的事情。
关于iphone - 以编程方式使用 ios-core 数据创建新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10890205/