如何使用 Core Data 存储 NSSet(一对多)?

发布于 2024-11-25 00:46:13 字数 1430 浏览 5 评论 0原文

我想知道如何以编程方式更新核心数据对象。不过该对象是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不美如何 2024-12-02 00:46:13
//call 
NSArray* oldTypos = [property.typology allObjects];
[[property removeTypology:[PropertyProvider sharedPropertyProvider].context];
[[property addTypology:storeSet];
for(int i = 0; i < [oldTypos count]; i++){
[[PropertyProvider sharedPropertyProvider].context deleteObject:[oldTypos:objectAtIndex:i]];
}
Error* error = nil;
if(![[PropertyProvider sharedPropertyProvider].context save:&error]){
abort();
}
//Also, rename the to-many relationship to plural.

如有任何错别字,敬请谅解。我现在在我的 Windows 机器上,所以我无法检查它。

//call 
NSArray* oldTypos = [property.typology allObjects];
[[property removeTypology:[PropertyProvider sharedPropertyProvider].context];
[[property addTypology:storeSet];
for(int i = 0; i < [oldTypos count]; i++){
[[PropertyProvider sharedPropertyProvider].context deleteObject:[oldTypos:objectAtIndex:i]];
}
Error* error = nil;
if(![[PropertyProvider sharedPropertyProvider].context save:&error]){
abort();
}
//Also, rename the to-many relationship to plural.

Apologies of there are any typos. I am on my windows machine at the moment so I cannot check it.

垂暮老矣 2024-12-02 00:46:13

TechZen 说:我刚刚注意到,我颠倒了家长描述的一对多关系。然而,一切都以同样的方式进行。当我有时间时我会进行编辑。

您正在努力手动完成 Core Data 自动执行的操作。要设置关系,您只需设置其一侧,托管对象上下文会自动设置另一侧。

因此,如果您有这样的数据模型:

Property{
    typology<<-->Property_has_typology.properties
}

Property_has_typology{
    properties<-->>Property.typology
}

然后要从您使用的 Property 对象端设置关系:

aPropertyObject.typology=aProperty_has_typologyObject;

要从您使用的 Property_has_typology 对象端设置关系Core Data 为您生成的实现 (.m) 中的关系访问器方法:

[aProperty_has_typologyObject addPropertiesObject:aPropertyObject];

[aProperty_has_typologyObject addPropertiesObjects:aSetOfPropertyObjects];

... 就完成了。

如果您必须手动管理所有对象关系,则核心数据不会提供太多实用性。

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:

Property{
    typology<<-->Property_has_typology.properties
}

Property_has_typology{
    properties<-->>Property.typology
}

Then to set the the relationship from the Property object side you just use:

aPropertyObject.typology=aProperty_has_typologyObject;

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:

[aProperty_has_typologyObject addPropertiesObject:aPropertyObject];

or

[aProperty_has_typologyObject addPropertiesObjects:aSetOfPropertyObjects];

... and you are done.

Core Data wouldn't provide much utility if you had to manage all the object relationships by hand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文