small medium large xlarge

Back to: All Forums  Core Data
Generic-user-small
11 Jan 2009, 17:11
Adam Venturella (4 posts)

This book has really helped me get into core data, which has been fantastic, no complaints from me. I was playing aorund with the recipes sample and was wondering if could create an auto-increment value. If nothing else, just pull some information from the last ingredient added to a recipe.

Digging around a bit, and using the platform of knowledge provided by this book, then pulling in some information, from the Aaron Hillegass book re: creating Managed Object Classes from the entities, plus a little googling:

http://lists.apple.com/archives/cocoa-dev/2008/Feb/msg00093.html (took a while to find this hahahahahaha)

I managed to create a solution where my Recipe Entity, has a to-one relationship ‘lastIngredient’ to my Ingredient Entity, (with no inverse)

The main issue I ran into was that the relationships are NOT available when the -(void)awakeFromInsert is called, instead I followed the link above, and implemented that solution.

My question is, is that the right approach, and will that adversely effect anything down the road? Also, perhaps it’s worth a mention/consideration in an advanced chapter, (i understand this is not yet in the beta book) that relationships are not available in -(void)awakeFromInsert

In the end my code to have a relationship so I could set lastIngredient in my recipe looks like this, which is directly based on that url above:

Ingredient.h (This is what X Code will generate By default)

#import <CoreData/CoreData.h>

@class Recipe;

@interface Ingredient :  NSManagedObject  
{
}

@property (retain) NSString * label;
@property (retain) Recipe * recipe;

@end

Ingredient.m (Here is where it takes from the link above)

#import "Ingredient.h"
#import "Recipe.h"


// coalesce these into one @interface Ingredient (CoreDataGeneratedPrimitiveAccessors) section
@interface Ingredient (CoreDataGeneratedPrimitiveAccessors)
- (Recipe *)primitiveRecipe;
- (void)setPrimitiveRecipe:(Recipe *)value;

@end


@implementation Ingredient 

@dynamic label;
@dynamic recipe;

-(void)awakeFromInsert
{
	[super awakeFromInsert];
	//NSLog(@"Awake From Insert: %@", self.recipe);
}

- (void)setRecipe:(Recipe *)value 
{
    /* Begin Apple Generated */
	[self willChangeValueForKey:@"recipe"];
    [self setPrimitiveRecipe:value];
    [self didChangeValueForKey:@"recipe"];
	/* End Apple Generated */
	
	/* begin object initialization here instead of awakeFromInsert since everything we want is now available */
        NSLog(@"%@", self.recipe.lastIngredient.label);
	
	// Both of these seem to work:
	//[self.recipe setValue:self forKey:@"lastIngredient"];
	self.recipe.lastIngredient = self;
	
}
@end

I got the added apple stuff from the data model: if you right click on the recipe relationship you will see: Coy Obj-C 2.0 Method Implementations to Clipboard. Then I followed the instructions in the post on how to implement. The cruxt being the category, and the - (void)setRecipe:(Recipe *)value;

A second question arises from a difference between Obj-C 1.0 and Obj-C 2.0. In the Apple documentation, it says that this is the way to access set the values: `[self.recipe setValue:self forKey:@”lastIngredient”];

I mentions the Obj-C 2.0 accessors as well. I was just wondering, for all intents and purposes re:core data, if this:

[self.recipe setValue:self forKey:@"lastIngredient"];` and `self.recipe.lastIngredient = self;` are treated exactly the same way by the Core Data framework?

Thanks again for all of your hard work, I can’t wait to learn more from this book!

You must be logged in to comment