【发布时间】:2011-12-01 21:13:53
【问题描述】:
我有 2 个实体 Locations 和 Items。多对多关系。
所以每个项目可以有多个位置,任何位置都可以有多个项目。
我正在解析项目的 XML,而不是尝试添加位置。
所以我有 ManagedObject 项目并且我刚刚插入了位置,那么设置项目位置的语法(代码)是什么?
我只看到在 ManagedObject 类中添加和删除。
以下是有效的 其中 currentItem,location 是两个 ManagedObjects,setItem_location_relationship 是 Items 实体中的关系名称
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Locations"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"location_id = %@",
[locationsArray objectAtIndex:i]];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
}
[fetchRequest release];
Locations *location = [fetchedObjects objectAtIndex:0];
NSSet *set = [NSSet setWithObject:location];
[currentItem setItem_location_relationship:set];
【问题讨论】:
标签: core-data many-to-many entity-relationship nsmanagedobject nsmanagedobjectcontext