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 hybriditem attribute items transformable attribute, have read stored "flat" blob of data - if modify item entity in later build , push update users, stored hybriditem cause problems when first fetch after updating

  • make hybriditem entity transient entity, , search combinable item objects each time app started. downside cause user wait combining of item objects

  • keep hybriditem regular class , search combinable items @ startup, same disadvantages above

  • keep hybriditem regular class , serialize separately using nscoding. 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 hybriditem subclass, define to-many relationship abstract item class
  • in item class, define to-one relationship called parentitem hybriditem class.
  • make these 2 relationships inverses of each other
  • in hybriditem class, define transient attributes values want compute based on amounts of each item in items array, such quantity of given unit.
  • as run algorithm gather combinable items single hybrid item, add items items collection associated hybriditem'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

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -