我有两个实体如图所示。我需要获取特定约会以及未删除与会者的所有相关与会者(hasDeleted!= 1)。我试过不同的谓词格式。以下是我尝试过的两个主谓词,并且无法使这些谓词中的任何一个起作用。你能帮我解决这个问题吗?
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"objectName = %@ AND objectId = %i AND SUBQUERY(attendees, $x, $x.hasDeleted != 1).@count != 0", [self.userDefaults stringForKey:OBJECT_NAME], [[self.userDefaults objectForKey:OBJECT_ID] intValue]];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"objectName = %@ AND objectId = %i AND (ANY attendees.hasDeleted != 1)", [self.userDefaults stringForKey:OBJECT_NAME], [[self.userDefaults objectForKey:OBJECT_ID] intValue]];
约会列表tableview
详细约会视图,可以看到与会者详细信息。
获取结果控制器代码:
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Attendee" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"uuid" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"appointment.objectName = %@ AND appointment.objectId = %i AND hasDeleted != 1", [self.userDefaults stringForKey:OBJECT_NAME], [[self.userDefaults objectForKey:OBJECT_ID] intValue]];
[fetchRequest setPredicate:predicate];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
答案 0 :(得分:1)
获取请求始终从托管对象上下文返回对象。你不能 fetch"修改过的对象",例如具有修改关系的约会对象 "与会者"仅包含具有特定属性的与会者。
您可以做的是改为取消删除 Attendee 对象 与给定的约会有关。 为Attendee实体创建获取请求并使用谓词
[NSPredicate predicateWithFormat:@"appointment.objectName = %@ AND appointment.objectId = %i AND hasDeleted != 1", [self.userDefaults stringForKey:OBJECT_NAME], [[self.userDefaults objectForKey:OBJECT_ID] intValue]]
更新:在您的第一个视图控制器中,您使用a 获取结果控制器,其中包含约会实体的获取请求。 如果您只想显示有任何未被取消的与会者的约会 然后添加谓词
[NSPredicate predicateWithFormat:"ANY attendees.hasDeleted != 1"]
到此获取请求。
当用户点击约会时,您将选择约会传递到下一个视图 控制器。要显示此约会的与会者,请使用提取的结果 控制器,具有 Attendee 实体的获取请求,并添加谓词
[NSPredicate predicateWithFormat:@"appointment = %@ AND hasDeleted != 1", selectedAppointment]