我想知道如何以編程方式更新核心數據對象。該對象雖然是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