views:

10

answers:

0

Hi all,

My application has two parts; a data importer part that takes a text file and creates Core Data objects from the data and then "saves" the data file (in this case, nutritional information for different foods). The data file is then used by the app.

The issue I'm running into is when I go to list the contents of a relationship, one of the elements seems to be missing. Specifically, in this case, each food item has a set of valid measurements (e.g. "grams," "cups," etc.) which are set in the data import.

However, when the app goes to list these out, one of the measurements ("grams") is always missing -- the rest, however, are present.

When I log the contents of the measurements set at the end of the import process everything looks fine. When file gets read in by the app and I run the same logging code, however, the measurement seems to be missing.

Is there something basic I'm overlooking? (relevant code added below)

    // create grams, which is the default serving unit
    FZData_ItemServingUnit *gramUnit = (FZData_ItemServingUnit *)[NSEntityDescription insertNewObjectForEntityForName:@"ItemServingUnit" inManagedObjectContext:context];                                                         
    gramUnit.name = @"g";
    gramUnit.equivalentAmtInGrams = [NSNumber numberWithInt:1];

    for (int i=0; i < 50; i++ ) {        

    //other stuff here...

    // create item serving units
    FZData_ItemServingUnit *defaultUnit = gramUnit; // default is grams

    // create a set, including grams, to start
    NSMutableSet *allowableUnits = [[NSMutableSet alloc] initWithObjects:gramUnit, nil];

    // iterate through item units allowed and create those
    for (int a = 0; a < [desc.weightNameArray count]; a++) {
        NSString *weightName = [desc.weightNameArray objectAtIndex:a];
        NSNumber *gramVal = [desc.weightGramValueArray objectAtIndex:a];

        FZData_ItemServingUnit *newUnit = (FZData_ItemServingUnit *)[NSEntityDescription insertNewObjectForEntityForName:@"ItemServingUnit" inManagedObjectContext:context];    
        newUnit.name = weightName;
        newUnit.equivalentAmtInGrams = gramVal; 

        // if custom units, then set this as the default unit
        if (a == 0) {
            defaultUnit = newUnit;
        }
        // add this to the set of allowable units
        [allowableUnits addObject:newUnit];
    }           

// other stuff here...

    newItem.defaultServingUnit = defaultUnit;
    [newItem addAllowedServingUnits:allowableUnits];        
}

So specifically, the gramUnit object doesn't seem to show up when allowedServingUnits is sent the allObjects: message in the app, though it does in the data import....

UPDATE: After further debugging, it looks like the "grams" value only shows up in the set of the last item that gets processed by the data importer. I've addressed the missing "grams" issue by creating a new NSManagedObject to represent "grams" for each item, but that seems inefficient. Still wondering if there's something I don't understand about how to use Core Data that's leading to this behavior....