我想知道如何以编程方式更新核心数据对象。该对象虽然是一个 NSSet。所以我可以用下面的方案来总结一下:
Property
---------
name
price
typology
Property_has_typology
---------------------
typology_id
property
Property 和 Property_has_typology 之间存在一对多的关系。因为一处房产可能有多种类型(又名类别),例如床和早餐、别墅、酒店、豪宅、乡间别墅。
所以我让用户在我的 TableView 中选择多行,当他点击保存时,我想存储这些更改。所以我这样做:
NSMutableArray *storeItems = [[NSMutableArray alloc] init];
//Get selected items
for (int i = 0; i < [items count]; i++) {
Properties_has_typology *typo = [NSEntityDescription insertNewObjectForEntityForName:@"Properties_has_typology"
inManagedObjectContext: [PropertyProvider sharedPropertyProvider].context];
typo.typology_id = [NSNumber numberWithInt: (int)[items objectAtIndex:i]];
typo.property = property;
[storeItems addObject: typo];
}
//Store the items for the Property and save it
if ([storeItems count] > 0) {
NSLog(@"Going to save...");
NSSet *storeSet = [[NSSet alloc] initWithArray:storeItems];
property.typology = storeSet;
[property save];
[storeSet release];
}
这有点工作,但问题是它并没有真正更新现有值。它只是覆盖它。因此,如果我两次保存相同的两项(仅作为示例),我将在我的数据库中获得以下内容:
PK TYPOLOGY
------------
1 |
2 |
3 | 4
4 | 6
所以是的,它们正在被存储,但它也会创建空行(清除它们而不是删除/更新它们)。
任何帮助,将不胜感激。谢谢!