views:

17

answers:

0

This code is used to make "initial data" or "test data".

The Category entity will carry default types. The Subcategory entity will copy default types from the Category. They wouldn't be using the same types because new or old types may be deleted or added to the Subcategory.

    -(void)newCat:(NSString*)catName subCat:(NSArray*)subCatNames types:(NSArray*)typeNames{
 //get the context
 NSManagedObjectContext *context = [self managedObjectContext];

 //make a category
 Category *theCat = [NSEntityDescription
      insertNewObjectForEntityForName:@"Category" 
      inManagedObjectContext:context];
 theCat.name = catName;

 //make default types and put it in the category
 for (int i = 0; i < [typeNames count]; i++) {
  Type *type = [NSEntityDescription
       insertNewObjectForEntityForName:@"Type" 
       inManagedObjectContext:context];
  type.name = [typeNames objectAtIndex:i];
  [theCat addTypesObject:type];
 }

 //make some subcategories
 //copy default types from category to subcategory
 for (int i = 0; i < [subCatNames count]; i++) {
  Subcategory *newSubcat = [NSEntityDescription
       insertNewObjectForEntityForName:@"Subcategory" 
       inManagedObjectContext:context];

  newSubcat.name = [logbookNames objectAtIndex:i];
  newSubcat.category = theCat;
  [newSubcat addTypes:theCat.types];
 }

 //save the context
 [self save];

 }

This method works the first time I open the app. Once I terminate and reopen the app, all but one Subcategory entity retains the types.

Why is the other Subcategory removing their types? I fear that I'm only setting a pointer to the default types in the Category.

How do I make a copy Category.types and set it as the types in Subcategory.types?

Design wise, should I create a new entity to hold default types for the Category instead of using the same entity for defaults and non-defaults?