views:

24

answers:

1

I have three core data entities which I am importing into, they are:

• sizes (Name, Clothes (one to many with "Clothes" entity))
• Clothes (Name, Price, etc... +2 relationships)
• shop (Name, Clothes (one to many to "Clothes" entity))

I have imported shops, into shop and sizes into the sizes entity.

Now I am importing the clothes. At the start I do a fetch request on the sizes and shops and store them to an array.

I am looping through all the clothes and populating the dictionary then I predicate on the array's for the for the shop name and size to get the managed object for each and then try and store that against the relationship and it does not work?

If though I do [arrShops objectAtIndex: x] and store that it works?

What is wrong with predicat'ing I cant get the correct managed object any other way can I?

Or is there another way of setting up this relationship?

Thanks James

EDIT


I have a method like this to get/predicate the shop:

- (void) getShops {

NSManagedObjectContext *moc = [(ImportingAppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext]; 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

[fetchRequest setEntity:[NSEntityDescription entityForName:@"Shop" inManagedObjectContext:moc]];

NSError *error;
arr_Manufacturers = [moc executeFetchRequest:fetchRequest error:&error];

NSLog(@"get: %@", [arr_Shops count]);

[fetchRequest release];
}

- (id) getShop:(NSString *)name {

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"Shop = %@", name];
return [arr_Shops filteredArrayUsingPredicate:predicate];

}

Then when trying to store i do the following:

 moc_Clothes.Shop = [self getShop:@"Shop Name Variable"];
A: 

First, it looks like your attributes aren't KVC compliant (important).

Second, there seems to be a disconnect you'll need to explain: you're filtering arr_Shops on a relationship but it's not clear where this array is coming from. In your getShops method (which oddly doesn't actually return anything), you assign the results of fetching all Shop instances to arr_Manufacturers.

I recommend a method named something like -shopsWithName: that takes a string name and returns an array of shops (always allow for the possibility of multiple results). In this method you run the fetch request with the shop name predicate.

Third is the predicate itself. You're asking for Shop instances that are equal to some string. None of them will be because they're Shops, not strings. Since your results are Shop instances, your predicate should match an attribute of that entity. Perhaps you meant "name == %@".

Joshua Nozzi
Should have waited 10 minutes before posting ;o) - Got it!Firstly I know I should return on the getShops, I am rushing quickly through this and I could have made a property and manually made the getter/setters.Now onto how I fixed it... changed get getShop return to "NSArray" return type instead of "id" and then did. [[self getShop:@"Name"] objectAtIndex:0];Relationships now setup. Cheers for your help!
jodm