如何使用 Core Data 存储 NSSet(一对多)?
我想知道如何以编程方式更新核心数据对象。不过该对象是一个 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
所以是的,它们正在被存储,但它也会创建空行(清除它们而不是删除/更新它们)。
任何帮助将不胜感激。谢谢!
I am wondering how I can programmatically update a Core Data object. The object is a NSSet though. So I can summarize this with the scheme below:
Property
---------
name
price
typology
Property_has_typology
---------------------
typology_id
property
There is a one-to-many relationship between the Property and Property_has_typology. As one property might have several typologies (aka categories) such as Bed & Breakfast, Villa, Hotel, Mansion, Country House.
So I let the user select multiple rows in my TableView and when he clicks save I want to store these changes. So I do:
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];
}
This kinda works, the issue though is that it doesn't really update the existing values. It just overrides it. So if I save the same two items twice (just as an example) I'd get the following in my database:
PK TYPOLOGY
------------
1 |
2 |
3 | 4
4 | 6
So yes they are being stored, but it also creates empty rows (clears them instead of deleting/updating them).
Any help would be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如有任何错别字,敬请谅解。我现在在我的 Windows 机器上,所以我无法检查它。
Apologies of there are any typos. I am on my windows machine at the moment so I cannot check it.
TechZen 说:我刚刚注意到,我颠倒了家长描述的一对多关系。然而,一切都以同样的方式进行。当我有时间时我会进行编辑。
您正在努力手动完成 Core Data 自动执行的操作。要设置关系,您只需设置其一侧,托管对象上下文会自动设置另一侧。
因此,如果您有这样的数据模型:
然后要从您使用的
Property
对象端设置关系:要从您使用的
Property_has_typology
对象端设置关系Core Data 为您生成的实现 (.m) 中的关系访问器方法:或
... 就完成了。
如果您必须手动管理所有对象关系,则核心数据不会提供太多实用性。
TechZen says: I just noticed after the fact that I reversed the to-many relationship that the parent described. However, everything works the same way. I'll edit when I find time.
You are working to hard by doing things manually that Core Data does automatically. To set a relationship you just set one side of it and the managed object context sets the other automatically.
So, if you have a data model like this:
Then to set the the relationship from the
Property
object side you just use:To set if from the
Property_has_typology
object side you use the relationship accessor methods in the implementation (.m) that Core Data generated for you:or
... and you are done.
Core Data wouldn't provide much utility if you had to manage all the object relationships by hand.