To avoid infinite recursion in the getter method, you have to use the "primitive accessor" Core Data accessor methods:
- (CacheControl *) cache {
[self willAccessValueForKey:@"cache"];
CacheControl * cache = [self primitiveValueForKey:@"cache"];
[self didAccessValueForKey:@"cache"];
if (cache == nil) {
cache = [NSEntityDescription insertNewObjectForEntityForName:@"CacheControl" inManagedObjectContext:self.managedObjectContext];
[self setPrimitiveValue:cache forKey:@"cache"];
}
return cache;
}
Similar examples can be found in the "Core Data Programming Guide" and in
the sectionIdentifier
method of the "Custom Section Titles with NSFetchedResultsController" sample project.