Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should Core Data be used with my "unusual" object?
    primarykey
    data
    text
    <p>I have a grocery list app, and I am planning on using Core Data with it.</p> <p>I have an grocery <code>Item</code> abstract class that has three subclasses: <code>RecipeItem</code>, <code>QuickAddItem</code>, and <code>HybridItem</code>. The first two subclasses are straightforward, but <code>HybridClass</code> is different - it's only ivar is an array of <strong>other</strong> <code>RecipeItem</code> and <code>QuickAddItem</code> objects.</p> <p>Here's how it's used: once the grocery list Item objects have been created, all <code>Item</code> objects with matching unit types (e.g. 1 lb. sugar, 1/2 lb. sugar) are put in the array <code>NSArray *items</code> and the <code>HybridItem</code> is initialized with the method:</p> <pre><code>- (id)initWithItems:(NSArray *)items </code></pre> <p>In this <code>initWithItems:</code> method, the items in the <code>items</code> array are massaged and combined so it will appear as a single entry in the grocery list. I need to hold on to the actual objects in order to refer to the recipe they originated from, the date they were added, and so on...</p> <p>As I start to plan the schema for Core Data, I am unsure about how to go about converting the <code>HybridItem</code> class to a NSManagedObject. The options I have considered:</p> <ul> <li><p>Make the <code>HybridItem</code> attribute <code>items</code> a <strong>transformable attribute</strong>, but from what I have read it is stored as a "flat" blob of data - if I modify my <code>Item</code> entity in a later build and push an update to my users, their stored <code>HybridItem</code> will cause problems when they do their first fetch after updating</p></li> <li><p>Make the <code>HybridItem</code> entity a <strong>transient entity</strong>, and search for combinable <code>Item</code> objects each time the app is started. The downside is this will cause the user to wait for the combining of <code>Item</code> objects</p></li> <li><p>Keep the <code>HybridItem</code> as a regular class and search for combinable Items at startup, same disadvantages as above</p></li> <li><p>Keep the <code>HybridItem</code> as a regular class and serialize separately using <code>NSCoding</code>. This has some code smell to be sure</p></li> </ul> <p>As I study questions similar to mine, the common wisdom is to take the attribute that are arrays like this and make it into a separate entity, and use relationships to associate with the object. But my case seems different, since the <code>items</code> attribute actually contains an array of entities with the same parent entity(!). </p> <p>I final consideration I have ruled out is just not to use core data. This will not work as my Items entity will have other entities with important relationships (amounts, units of measurement) that would make <strong>not</strong> using Core Data unfeasible. </p> <p>How should I code my <code>HybridItem</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload