Ios CoreData:反向关系导致保存时崩溃
我的应用程序中有一些代码生成核心数据模型,并用一组Ios CoreData:反向关系导致保存时崩溃,ios,objective-c,core-data,Ios,Objective C,Core Data,我的应用程序中有一些代码生成核心数据模型,并用一组NSEntityDescriptions填充它。然后,这些实体描述中的每一个都会为其分配任意数量的NSPropertyDescriptions。这些属性是NSAttributeDescriptions和nsrrelationshipsdescriptions的组合。所有关系都与现有关系匹配,并使用setInversionRelationship:将它们设置为彼此相反的关系 属性属性工作正常,对许多关系工作正常;我已经彻底测试过了。问题似乎在于ns
NSEntityDescription
s填充它。然后,这些实体描述中的每一个都会为其分配任意数量的NSPropertyDescription
s。这些属性是NSAttributeDescription
s和nsrrelationshipsdescription
s的组合。所有关系都与现有关系匹配,并使用setInversionRelationship:
将它们设置为彼此相反的关系
属性属性工作正常,对许多关系工作正常;我已经彻底测试过了。问题似乎在于nsrrelationshipDescription
s的maxCount
值为1,这意味着属性描述返回的istomy
值为NO
。当为这些类型的关系设置了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
我从中得到的暗示是,它认为反向关系是一种对多关系,而事实并非如此。根据我的断言,我对我的每一段关系的了解是:
relationDescription.inverseRelationship != nil
[relationDescription.inverseRelationship.inverseRelationship isEqual:relationDescription]
我通过创建模型并用一组小样本数据填充它来测试这一点。当前,具有任何类型属性的对象、到多个关系(带/不带逆)和到一个关系(不带逆)的对象都可以一致地工作。当我试图与一对一的关系与一个反向关系时,问题就出现了
这似乎有点复杂,所以如果需要进一步澄清,请告诉我。谢谢
编辑1:
关系创建分两步完成,首先创建所有关系,然后使用查找建立每个关系的逆关系
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让一些东西点击。因此,从外观上看,如果您对一个
NSRelationDescription
关系调用setOrdered:
,核心数据的内部自动将其视为一个对多关系,尽管与maxCount
为一的冲突
我通过将关系的创建代码更改为:
NSRelationshipDescription *description = [[NSRelationshipDescription alloc] init];
[description setName:self.name];
[description setDestinationEntity:entity];
[description setMaxCount:(isToMany ? 0 : 1)];
[description setMinCount:0];
[description setOrdered:YES];
致:
你能发布创建模型的代码吗?也许会减少到一个显示问题的最小示例?该死,谢谢!那使我省去了很多头痛。如果您在Xcode中使用模型编辑器,似乎也是如此。如果切换到“多”,请取消选中Ordered标志,然后切换回,这样可以修复此错误。
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];