我正在尝试为一个对象建立关系。但我想我没有正确理解所有内容。当我设置它们时,看起来它们都设置正确(即找到了关系)。但是当我稍后遍历它们时,其中一些(不是全部)不存在。
我有一个Report
,里面有很多LineItems
。每项检验项目一项Rating
。当我创建检查时,我正在从另一个对象复制数据,ReportMaster
. ReportMaster 也有 MasterLineItems 也有评级。但截至目前,我不能保证 MasterLineItems 和 Ratings 之间的关系是正确的。因此,当我为报告复制 ReportMaster 的属性时,我也会获取评级。
我在 Report 中有一个方法来检查是否所有 LineItems 都有评级。但我永远无法让它返回真实。
我的问题是,我应该如何正确设置这些属性,或者我做错了什么?谢谢。(代码块末尾的关系建立)
# Creating Report
Report *report = [Report object];
[report setWithMasterReport:masterReport];
# Report.m
- (void)setWithMasterReport:(MasterReport *)masterReport {
self.name = [masterReport.name copy];
[self addLineItems:[LineItems copyItemsFromMasterReportItems:masterReport.masterLineItems]];
NSLog(@"items have ratings: %i",[self lineItemsHaveRatings]);
}
- (BOOL)lineItemsHaveRatings {
NSArray *lineItems = [[self lineItems] allObjects];
BOOL t = TRUE;
for (LineItem *lineItem in lineItems){
if (lineItem.rating)
t = t && TRUE;
else
t = FALSE;
}
return t;
}
# LineItem.m
+ (NSSet*)copyItemsFromMasterReportItems:(NSSet *)masterLineitems {
NSMutableArray *lineItems = [NSMutableArray arrayWithArray:[masterLineitems allObjects]];
// go through one by one and replace inspectionFormItem with the inspectionItem copy
for (NSUInteger i; i < [lineItems count]; i++){
LineItem *lineItem = [InspectionItem object];
[lineItem setWithMasterLineItem:[lineItems objectAtIndex:i]];
[lineItems replaceObjectAtIndex:i withObject:lineItem];
}
return [NSSet setWithArray:lineItems];
}
- (void)setWithMasterLineItem:(MasterLineItem *)masterLineItem {
self.name = masterLineItem.name;
self.ratingID = masterLineItem.ratingID;
Rating *rating = [Rating findFirstByAttribute:@"myID" withValue:self.ratingID];
if (rating)
[self setRating:rating];
}
当我跑步时,我得到
items have ratings: 0
这个问题的另一部分是,在这些“init”方法(setWith...:
)中,我是否应该复制属性?如果是这样,我也应该复制这种关系吗?我只想要一个指向对象的指针,对吧?
旁注:这些是 CoreData 对象,我正在使用 RestKit,这就是我获得findFirstByAttribute:withValue:
辅助方法的原因。
谢谢你的帮助。
更新
- (BOOL)LineItemsHaveRatings {
NSArray *lineItems = [[self LineItems] allObjects];
BOOL t = TRUE;
for (LineItem *lineItem in lineItems){
if (lineItem.rating)
t = t && TRUE;
else {
t = FALSE;
Rating *rating = [Rating findFirstByAttribute:@"myID" withValue:LineItem.ratingID];
NSLog(@"[No Rating Found] %@",lineItem);
NSLog(@"[Rating to find] %@",rating);
}
}
return t;
}
[No Rating Found] <LineItem: 0x8200e40> (entity: LineItem; id: 0x8200d00 <x-coredata:///LineItem/t05319326-640E-4DA5-B619-EB20621E3D533> ; data: {
rating = nil;
ratingID = 13903;
})
[Rating to find] <Rating: 0xec356a0> (entity: Rating; id: 0xec31520 <x-coredata://4EE6AD5A-CC34-460A-A97A-0909454126A4/Rating/p15553> ; data: {
myID = 13903;
name = "APPA (Northwestern Memorial Hospital)";
rangeItemRatings = (
"0xec3a2d0 <x-coredata://4EE6AD5A-CC34-460A-A97A-0909454126A4/RangeItemRating/p2952>",
"0xec3a2b0 <x-coredata://4EE6AD5A-CC34-460A-A97A-0909454126A4/RangeItemRating/p2950>",
"0xec3a2e0 <x-coredata://4EE6AD5A-CC34-460A-A97A-0909454126A4/RangeItemRating/p2953>",
"0xec3a2f0 <x-coredata://4EE6AD5A-CC34-460A-A97A-0909454126A4/RangeItemRating/p2954>",
"0xec3a2c0 <x-coredata://4EE6AD5A-CC34-460A-A97A-0909454126A4/RangeItemRating/p2951>"
);
ratingType = "0xec368b0 <x-coredata://4EE6AD5A-CC34-460A-A97A-0909454126A4/RatingType/p112>";
ratingTypeID = 1;
})
令我感到奇怪的是,其他关系是建立并保持联系的。但是这个没有。... 哦。我没有将关系设置为一对多关系。哎呀。