在我的Lion应用中,我有这样的数据模型:

Item内的关系子项是有序的。

Xcode 4.1 (build 4B110)为我创建了文件Item.h, Item.h。m, SubItem.h和SubItem.h。

下面是Item.h的内容(自动生成):

#import <Foundation/Foundation.h>

#import <CoreData/CoreData.h>

@class SubItem;

@interface Item : NSManagedObject {
@private
}

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSOrderedSet *subitems;
@end

@interface Item (CoreDataGeneratedAccessors)

- (void)insertObject:(SubItem *)value inSubitemsAtIndex:(NSUInteger)idx;
- (void)removeObjectFromSubitemsAtIndex:(NSUInteger)idx;
- (void)insertSubitems:(NSArray *)value atIndexes:(NSIndexSet *)indexes;
- (void)removeSubitemsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectInSubitemsAtIndex:(NSUInteger)idx withObject:(SubItem *)value;
- (void)replaceSubitemsAtIndexes:(NSIndexSet *)indexes withSubitems:(NSArray *)values;
- (void)addSubitemsObject:(SubItem *)value;
- (void)removeSubitemsObject:(SubItem *)value;
- (void)addSubitems:(NSOrderedSet *)values;
- (void)removeSubitems:(NSOrderedSet *)values;

@end

下面是Item.m的内容(自动生成):

#import "Item.h"
#import "SubItem.h"

@implementation Item

@dynamic name;
@dynamic subitems;

@end

如您所见,Item类提供了一个名为addSubitemsObject:的方法。不幸的是,当你试图以这种方式使用它时:

Item *item = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:self.managedObjectContext];
item.name = @"FirstItem";

SubItem *subItem = [NSEntityDescription insertNewObjectForEntityForName:@"SubItem" inManagedObjectContext:self.managedObjectContext];

[item addSubitemsObject:subItem];

出现以下错误:

2011-09-12 10:28:45.236 Test[2002:707] *** -[NSSet intersectsSet:]: set argument is not an NSSet

你能帮我吗?

更新:

在我的错误报告刚刚过去1787天之后,今天(2016年8月1日),苹果公司给我写了这样的话:“请用最新的iOS 10测试版验证这个问题,并在bugreport.apple.com上更新你的错误报告。”让我们希望这是正确的时间:)


当前回答

苹果文档To Many Relations说:你应该访问代理可变集或有序集使用

NSMutableOrderedSet * set = [managedObject mutableOrderedSetValueForKey:@"toManyRelation"];

修改此集合将添加或删除与托管对象的关系。使用[]或访问器访问可变有序集。符号是错误的,会失败。

其他回答

我发现使用LeeIII的方法是有效的,但在分析时发现它非常慢。解析1000个条目花了15秒。注释出代码以添加关系将15秒变成2秒。

我的解决方案(更快,但更丑陋)包括创建一个临时可变数组,然后在所有解析完成后将其复制到有序集中。(如果您要添加许多关系,这只是性能上的优势)。

@property (nonatomic, retain) NSMutableArray* tempItems;
 ....
@synthesize tempItems = _tempItems;
 ....

- (void) addItemsObject:(KDItem *)value 
{
    if (!_tempItems) {
        self.tempItems = [NSMutableArray arrayWithCapacity:500];
    }
    [_tempItems addObject:value];
}

// Call this when you have added all the relationships
- (void) commitRelationships 
{
    if (_tempItems) {
        self.items = [NSOrderedSet orderedSetWithArray:self.tempItems];
        self.tempItems = nil;
    }
}

我希望这能帮助到其他人!

我通过谷歌错误信息找到了这个问题,只是想指出我以一种稍微不同的方式(不是使用有序集)遇到了这个错误。这并不完全是对给定问题的答案,但我把它贴在这里,以防它对其他在搜索时偶然发现这个问题的人有帮助。

我正在添加一个新的模型版本,并向现有模型添加了一些关系,并在头文件中自己定义了add*Object方法。当我尝试调用它们时,我得到了上面的错误。

在检查了我的模型之后,我意识到我愚蠢地忘记了勾选“to - many Relationship”复选框。

所以如果你遇到这种情况,你没有使用有序集,仔细检查你的模型。

我刚刚遇到了这个问题,并使用一个比这里介绍的其他实现简单得多的实现解决了这个问题。我只是使用NSManagedObject上可用的方法来处理不使用子类时的关系。

一个插入实体到NSOrderedSet关系的示例实现如下所示:

- (void)addAddress:(Address *)address
{
    if ([self.addresses containsObject:address]) {
        return;
    }
    // Use NSManagedObject's methods for inserting an object
    [[self mutableOrderedSetValueForKey:@"addresses"] addObject:address];
}

这是完美的工作,并且是我在移动到NSManagedObject子类之前使用的。

相反,我建议使用NSObject中的访问器来访问关系的NSMutableOrderedSet。

- (void)addSubitemsObject:(SubItem *)value {
      NSMutableOrderedSet* tempSet = [self mutableOrderedSetValueForKey:@"subitems"];
      [tempSet addObject:value];
 }

例如,iOS v5.0的核心数据发布说明参考了这一点。

在一个简短的测试中,它在我的应用程序中工作。

似乎如果你通过设置父节点和子节点而不是相反的方式来连接父节点和子节点,它就不会崩溃。

所以如果你这样做:

[child setParent:parent]

而不是

[parent setChildObects:child]

它应该可以工作,至少在iOS 7上可以工作,而且关系没有任何问题。