需要帮助在iPhone的核心数据多对多关系 [英] Need help with many-to-many relationships in core data for iPhone
问题描述
我在我目前的项目中来了一个路障。我基本上有一个应用程序,就像Core Data Recipe应用程序...
这是我的.xcdatamodel中的基本结构
实体:
String:name
类别:category< ---- to-many relationship
实体:类别
String:name
餐厅:餐厅< ---- to-many关系
所以基本上,一个餐厅可以有多个类别...
和有一个const数量的预定义类别..
例如:
餐厅:
名称:Chili's
分类:外卖,家庭用餐
外卖和家庭餐饮是10的不同可能的餐厅类别中的2。
我如何做这个..我看过sqllite数据库,我有我的ZRestaurant和ZCategory表+连接表为他们...我有两个数据...
如何设置我的餐厅Catagory与不同的值?然后如何取回他们?
感谢所有!
Kurt
确定,在过去2天的工作后,我终于想出了我的解决方案,实际上是亚历克斯和威尔斯的建议之间的混合...谢谢你们两个!
这是我有...
NSManagedObjectContext * context = [restaurant managedObjectContext];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@CategoryinManagedObjectContext:context]];
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@nameascending:YES];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:& sortDescriptor count:1];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError * error = nil;
NSArray * possibleCategories = [context executeFetchRequest:fetchRequest error:& error];
categoryArray = [[NSMutableArray alloc] initWithArray:possibleCategories];
currentCategories = [restaurant valueForKeyPath:@categories];
[restaurant addCategoriesObject:(Category *)[possibleCategories objectAtIndex:15]];
[currentCategories addObject:(Category *)[categoryArray objectAtIndex:15]];
然后我这样保存
- (void)save {
NSLog(@EditCatagoriesTableViewController - save);
NSSet * myCategorySet = [[NSSet alloc] initWithSet:currentCategories];
NSError * error = nil;
[restaurant addCategories:myCategorySet];
error = nil;
if(![restaurant.managedObjectContext save:& error]){
//处理错误
NSLog(@restaurant - 未解决的错误%@,%@,错误,[错误userInfo]);
exit(-1); // Fail
}
}
>
非常感谢您的帮助。
-Kurt
I have come to a roadblock in my current project. I basically have an app that is much like the Core Data Recipe app... Here is the basic structure I have in my .xcdatamodel
Entity: Restaurant String: name Category: category <---- to-many relationship
Entity: Category String: name Restaurant: restaurant <---- to-many relationship So basically, a Restaurant can have multiple categories... And there are a const number of predefined Categories.. For example: Restaurant: Name: Chili’s Categories: Take Out , Family Dining
"Take Out" and "Family Dining" are 2 of 10 different possible Restaurant Categories. How do I go about doing this.. I have looked at the sqllite database and I have my ZRestaurant and ZCategory table + the join table for both of them... I have data in both...
How do I go about setting my Restaurants Catagory with the different values? and then how do I fetch them back?
Thanks all! Kurt
OK, After working on this for the past 2 days I finally came up with my solution which was actually a mix between Alex and Wills suggestions... Thank you to both of you!!
Here is what I have...
NSManagedObjectContext *context = [restaurant managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:context]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *possibleCategories = [context executeFetchRequest:fetchRequest error:&error];
categoryArray = [[NSMutableArray alloc] initWithArray:possibleCategories];
currentCategories = [restaurant valueForKeyPath:@"categories"];
[restaurant addCategoriesObject:(Category *)[possibleCategories objectAtIndex:15 ]];
[currentCategories addObject:(Category*)[categoryArray objectAtIndex:15]];
and then I save like this
- (void)save{
NSLog(@"EditCatagoriesTableViewController - save");
NSSet* myCategorySet = [[NSSet alloc] initWithSet:currentCategories];
NSError *error = nil;
[restaurant addCategories:myCategorySet];
error = nil;
if (![restaurant.managedObjectContext save:&error]) {
// Handle error
NSLog(@"restaurant - Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
And that does it!
Thank you so much for the help you two!!!
-Kurt
这篇关于需要帮助在iPhone的核心数据多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!