我想知道如何以编程方式更新核心数据对象。该对象虽然是NSSet。因此,我可以用下面的方式总结一下:如何使用核心数据存储NSSet(一对多)?
Property
---------
name
price
typology
Property_has_typology
---------------------
typology_id
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
所以,是的,他们正在储存,但是也带来了空行(清除它们而不是删除/更新它们)。
任何帮助,将不胜感激。谢谢!
看起来''deleteObject'确实是我正在寻找的东西。现在我可以完全删除所有条目并插入新条目。谢谢! – Jules