NSFetchResultsController 的 NSPredicate
NSPredicate for a NSFetchResultsController
我正在尝试为我在 UITableView
中使用的 NSFetchResultsController
创建一个 NSPredicate
。
我有 3 个实体,User
(1 对多)-> Visit
(1 对多)-> Treatment
,并且想要获得用户拥有的所有治疗。
我尝试使用子查询 SELF
、ANY
,但只得到 return 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);
}
或者,对于获取的结果控制器,您使用 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);
}
您需要使用Treatment
作为要获取的实体。用户的参考两个级别应该是微不足道的,因为反向关系是一对一的。
正确的谓词应该是这样的
NSPredicate(format: "visit.user = $@", user)
您的日志表明您获取的是就诊,而不是治疗。
我正在尝试为我在 UITableView
中使用的 NSFetchResultsController
创建一个 NSPredicate
。
我有 3 个实体,User
(1 对多)-> Visit
(1 对多)-> Treatment
,并且想要获得用户拥有的所有治疗。
我尝试使用子查询 SELF
、ANY
,但只得到 return 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);
}
或者,对于获取的结果控制器,您使用 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);
}
您需要使用Treatment
作为要获取的实体。用户的参考两个级别应该是微不足道的,因为反向关系是一对一的。
正确的谓词应该是这样的
NSPredicate(format: "visit.user = $@", user)
您的日志表明您获取的是就诊,而不是治疗。