ios - How should Core Data be used with my "unusual" object? -
i have grocery list app, , planning on using core data it.
i have grocery item abstract class has 3 subclasses: recipeitem, quickadditem, , hybriditem. first 2 subclasses straightforward, hybridclass different - it's ivar array of other recipeitem , quickadditem objects.
here's how it's used: once grocery list item objects have been created, item objects matching unit types (e.g. 1 lb. sugar, 1/2 lb. sugar) put in array nsarray *items , hybriditem initialized method:
- (id)initwithitems:(nsarray *)items in initwithitems: method, items in items array massaged , combined appear single entry in grocery list. need hold on actual objects in order refer recipe originated from, date added, , on...
as start plan schema core data, unsure how go converting hybriditem class nsmanagedobject. options have considered:
make
hybriditemattributeitemstransformable attribute, have read stored "flat" blob of data - if modifyitementity in later build , push update users, storedhybriditemcause problems when first fetch after updatingmake
hybriditementity transient entity, , search combinableitemobjects each time app started. downside cause user wait combining ofitemobjectskeep
hybriditemregular class , search combinable items @ startup, same disadvantages abovekeep
hybriditemregular class , serialize separately usingnscoding. has code smell sure
as study questions similar mine, common wisdom take attribute arrays , make separate entity, , use relationships associate object. case seems different, since items attribute contains array of entities same parent entity(!).
i final consideration have ruled out not use core data. not work items entity have other entities important relationships (amounts, units of measurement) make not using core data unfeasible.
how should code hybriditem?
it seems straightforward approach follows:
- in
hybriditemsubclass, define to-many relationship abstractitemclass - in
itemclass, define to-one relationship calledparentitemhybriditemclass. - make these 2 relationships inverses of each other
- in
hybriditemclass, define transient attributes values want compute based on amounts of each item initemsarray, such quantity of given unit. - as run algorithm gather combinable items single hybrid item, add items
itemscollection associatedhybriditem's to-many relationships
note approach allow hybrid item include other hybrid items. if don't want that, have introduce new subclass of item , call singleitem. make recipeitem , quickadditem (but not hybriditem) subclasses of singleitem. create relationships described above, instead create them between singleitem , hybriditem
Comments
Post a Comment