CoreData:导致保存时崩溃的反向关系

时间:2013-11-09 18:48:43

标签: ios objective-c core-data

我的应用程序中有一些代码生成一个核心数据模型并用一组NSEntityDescription填充它。然后,这些实体描述中的每一个都具有为其分配的任意数量的NSPropertyDescription。这些属性是NSAttributeDescriptionNSRelationshipDescription的组合。所有关系都与现有关系匹配,并使用setInverseRelationship:将它们设置为彼此相反。

属性属性工作正常,并且多对多关系工作正常;我已经彻底测试过了。问题似乎是NSRelationshipDescription的{​​{1}}值为1,这意味着属性说明的maxCount值为isToMany。当为这些类型的关系设置NO属性时,当我尝试将任何利用该关系的对象保存为错误时,Core Data崩溃:

inverseRelationship

我从中收集的暗示是,当不是这种情况时,它正在考虑反向关系是多对多关系。根据我的断言,我对每个人际关系的了解是:

2013-11-09 11:17:15.068 Directory[1344:5c03] -[NSManagedObject count]: unrecognized selector sent to instance 0x9643820
2013-11-09 11:17:15.074 Directory[1344:5c03] *** Terminating app due to uncaught exception  'NSInvalidArgumentException', reason: '-[NSManagedObject count]: unrecognized selector sent to instance 0x9643820'
*** First throw call stack:
(
    0   CoreFoundation                      0x01f9f5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01d228b6 objc_exception_throw + 44
    2   CoreFoundation                      0x0203c903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x01f8f90b ___forwarding___ + 1019
    4   CoreFoundation                      0x01f8f4ee _CF_forwarding_prep_0 + 14
    5   CoreData                            0x0083c128 -[NSSQLCore _knownOrderKeyForObject:from:inverseToMany:] + 200
    6   CoreData                            0x00773080 -[NSSQLCore _populateRowForOp:withObject:] + 1120
    7   CoreData                            0x00789157 -[NSSQLCore recordValuesForInsertedObject:] + 71
    8   CoreData                            0x00771e8d -[NSSQLCore recordChangesInContext:] + 685
    9   CoreData                            0x00770c55 -[NSSQLCore saveChanges:] + 565
    10  CoreData                            0x0073d88c -[NSSQLCore executeRequest:withContext:error:] + 412
    11  CoreData                            0x0073d380 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 4704
    12  CoreData                            0x00769ffc -[NSManagedObjectContext save:] + 764
    13  Directory                           0x0002f2bf -[ETDatabaseController save] + 111
    14  Directory                           0x0002ba9c -[ETDataLoader performImportWithData:] + 10284

我已经通过创建模型并使用一小组样本数据填充它来测试它。目前,具有任何类型属性,多对多关系(有/无反向)和一对一关系(无反向)的对象一致地工作。当我试图与反向关系建立一对一的关系时会出现问题。

这似乎有点令人费解,所以如果我需要澄清更好的话,请告诉我。谢谢!

修改1:

关系创建分两步完成,首先创建所有关系,然后通过查找建立每个关系的反转。

relationDescription.inverseRelationship != nil
[relationDescription.inverseRelationship.inverseRelationship isEqual:relationDescription]

后来...

NSRelationshipDescription *description = [[NSRelationshipDescription alloc] init];
[description setName:self.name];
[description setDestinationEntity:entity];
[description setMaxCount:(isToMany ?  0 : 1)];
[description setMinCount:0];
[description setOrdered:YES]; // See you in a minute

1 个答案:

答案 0 :(得分:3)

好吧,我猜编辑1点击了一下。所以从它的外观来看,如果你在一个setOrdered:上调用NSRelationDescription这是一个一对一的关系,那么Core Data的内部会自动将它视为一种多对多的关系,尽管存在冲突maxCount是一个。

我通过更改关系的创建代码来修复此问题:

NSRelationshipDescription *description = [[NSRelationshipDescription alloc] init];
[description setName:self.name];
[description setDestinationEntity:entity];
[description setMaxCount:(isToMany ?  0 : 1)];
[description setMinCount:0];
[description setOrdered:YES];

为:

NSRelationshipDescription *description = [[NSRelationshipDescription alloc] init];
[description setName:self.name];
[description setDestinationEntity:entity];
[description setMaxCount:(isToMany ?  0 : 1)];
[description setMinCount:0];

if (isToMany)
    [description setOrdered:YES];
相关问题