我正在尝试为NSPredicate
中使用的NSFetchResultsController
创建一个UITableView
。
我有3个实体,User
(1到多个) - > Visit
(1对多) - > Treatment
,并希望获得用户拥有的所有治疗方法。
我尝试过使用子查询SELF
,ANY
,只返回Visits
而不是Treatments
。
有人可以指导我如何从用户实体访问治疗实体吗?
提前感谢您的帮助!
修改
这是我目前用来尝试获取处理的代码
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:kTreatmentVisits];
fetchRequest.sortDescriptors = descriptors;
fetchRequest.fetchBatchSize = 10;
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"ANY treatmentVisits = %@.visitTreatment", user.userVisit];
结果我得到了访问,而不是治疗
<NSFetchRequest: 0x60b000148a40> (entity: treatmentVisits; predicate: (ANY treatmentVisits == {<Visits: 0x60b0000a0ad0> (entity: VisitsEntity; id: 0xd0000000000c000a <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p3> ; data: {
visitCause = "";
visitEndDate = nil;
visitId = nil;
visitRecommendation = "";
visitStartDate = "2016-08-11 04:43:50 +0000";
visitTreatment = "<relationship fault: 0x60300025e6d0 'visitTreatment'>";
visitType = 0;
visitUser = "0xd000000001940000 <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/UsersEntity/p101>";
}), <Visits: 0x60b0000bec10> (entity: VisitsEntity; id: 0xd00000000008000a <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p2> ; data: {
visitCause = "";
visitEndDate = nil;
visitId = nil;
visitRecommendation = "";
visitStartDate = "2016-08-04 20:26:33 +0000";
visitTreatment = "<relationship fault: 0x60300025e850 'visitTreatment'>";
visitType = 0;
visitUser = "0xd000000001940000 <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/UsersEntity/p101>";
}), <Visits: 0x60b000148570> (entity: VisitsEntity; id: 0xd00000000010000a <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p4> ; data: <fault>), <Visits: 0x60b000148620> (entity: VisitsEntity; id: 0xd00000000004000a <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p1> ; data: <fault>)}.visitTreatment); sortDescriptors: ((
"(treatmentStartTime, descending, compare:)"
)); batch size: 10; type: NSManagedObjectResultType; )
我希望能够访问 visitTreatment
中的内容我也试过
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", user.userVisit];
具有相同的结果。
答案 0 :(得分:2)
首先,如果您想获得治疗,FRC的基础提取必须指定治疗实体:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"TreatmentsEntity"];
然后通过指定谓词将结果限制为给定UsersEntity
的处理,例如requiredUser
:
fetchRequest.predicate = [NSPredicate predicateWithFormat:"treatmentVisits.visitUser == %@", requiredUser];
然后,您需要直接执行获取请求:
NSError *error = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (results == nil) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
} else {
NSLog(@"Results is %@",results);
}
或者,对于获取的结果控制器,使用fetchRequest初始化FRC,然后调用performFetch:
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
NSError *error = nil;
if (![frc performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
} else {
NSLog(@"Results %@",frc.fetchedObjects);
}
答案 1 :(得分:0)
您需要使用Treatment
作为要获取的实体。直到用户的参考两个级别应该是微不足道的,因为反向关系是一个。
正确的谓词就像是
NSPredicate(format: "visit.user = $@", user)
您的日志表明您正在访问,而非治疗。