我正在尝试为我在 UITableView
中使用的 NSFetchResultsController
创建一个 NSPredicate
。
我有 3 个实体,User
(一对多)-> Visit
(一对多)-> 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];
结果相同。
首先,如果您想获取治疗,您的 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);
}
或者,对于获取的结果 Controller ,您使用 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);
}
关于ios - NSFetchResultsController 的 NSPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39173965/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |