How to properly set relationships in Core Data when using setValue and data already exists

Posted by ern on Stack Overflow See other posts from Stack Overflow or by ern
Published on 2010-06-09T04:29:15Z Indexed on 2010/06/09 4:32 UTC
Read the original article Hit count: 267

Filed under:
|
|

Let's say I have two objects: Articles and Categories. For the sake of this example all relevant categories have already been added to the data store. When looping through data that holds edits for articles, there is category relationship information that needs to be saved.

I was planning on using the -setValue method in the Article class in order to set the relationships like so:

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    if([key isEqualToString:@"categories"]){
        NSLog(@"trying to set categories...");
    }
}

The problem is that value isn't a Category, it is just a string (or array of strings) holding the title of a category. I could certainly do a lookup within this method for each category and assign it, but that seems inefficient when processing a whole bunch of articles at once. Another option is to populate an array of all possible categories and just filter, but my question is where to store that array? Should it be a class method on Article? Is there a way to pass in additional data to the -setValue method? Is there another, better option for setting the relationship I'm not thinking of?

Thanks for your help.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c