开发者

How to dynamically change Core Data entity column type?

How can I change the type of a Core Data entity column programmatically? For example, form Strin开发者_JAVA技巧g to Int 16. The entity can be assumed empty (no data row).


Firstly, it's not an SQL column, it's the attribute of an entity. Attributes have behaviors, columns in SQL do not.

In answer to your question, once a data model model has been used to write data to a persistent store file (sqlite store or other formats) it cannot altered programatically. If you change an attribute, you need to perform a version migration.

See: Core Data Model Versioning and Data Migration Programming Guide for details.

A little general advice: Core Data is not SQL. Entities are not tables. Objects are not rows. Attributes are not columns. Relationships are not joins. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. Trying to think of Core Data in SQL terms will cause you to completely misunderstand Core Data and result in much grief and wasted time.


You can manipulate the attributes (including) type of an entity in an NSManagedObjectModel until it is used to initialize a persistent store coordinator. So, create the NSManagedObjectModel, mutate the attribute (not a column) type, then setup the Core Data stack as usual.

Of course, if you already have any data persisted using the original model, you'll have to perform a schema migration to update the data to the new type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜