views:

45

answers:

1

Hello.. I'm having a problem with relationships between to entities in Core Data. I'm parsing some JSON and adding the entities:

if ([hourSets isKindOfClass:[NSArray class]]) { // check to see that we have got some hours back

    for (NSDictionary *hourSet in hourSets) {

        Hourset *thisHourSet = (Hourset *)[NSEntityDescription
               insertNewObjectForEntityForName:@"Hourset"
               inManagedObjectContext:managedObjectContext];

        [thisHourSet setStartDate:[hourSet objectForKey:@"start_date"]];
        [thisHourSet setEndDate:[hourSet objectForKey:@"end_date"]];
        [record addHoursetsObject:thisHourSet];

    }

}

...and then later trying to grab them again:

NSSet *hourSets = [self.listing valueForKeyPath:@"hoursets.hourset"];   
NSLog(@"There are %@ hourSets", [hourSets count]);

I'm getting Program received signal: “EXC_BAD_ACCESS”. when trying to access that hourSets NSSet in any way, including just counting the items in it.

Any suggestions? Pretty stumped. Thanks!

+1  A: 

I am inferring your entity graph here but:

[self.listing valueForKeyPath:@"hoursets.hourset"]

... translates to a keypath of listing.hoursets.hourset which does not appear to return a set. Both the first and last elements are singular and therefore by convention not sets.

I would suggest logging the class of the return to confirm what, if anything, you're getting back.

Update:

(Forehead slap) The problem is actually the log statement itself. It should be:

NSLog(@"There are %d hourSets", [hourSets count]);

... because count returns an NSUInteger.

TechZen
I get __NSCFSet for listing.hoursets.hourset and _NSFaultingMutableSet for listing.hoursets. Both fail when I try to count what was returned.
Nick
Well, I feel stupid. The problem is actually the log statement. see the update.
TechZen
You feel stupid? :) Good lesson for me: when debugging my problems, maybe it's my debugging that's causing the problems. Thanks for the help.
Nick