我开始使用获取请求模板,因此我会尽力解释。请询问是否需要更多详细信息。
我创建了一个获取请求模板:
并尝试在下面的方法中打印所有对象:
- (void)printOfflineActivities
{
EntityManager* entityManager = [[EntityManager alloc] init];
ActivityFetcher *activityFetcher = [[ActivityFetcher alloc] initWithEntityManager:entityManager];
Activity *activity = [activityFetcher activitiesOffline:[NSNumber numberWithBool:YES]];
NSLog(@"printing..%@",activity.name);
}
活动代码是
- (Activity*)activitiesOffline:(NSNumber*)offline
{
NSDictionary* variables = [NSDictionary dictionaryWithObject:offline forKey:OfflineTemplatePlaceholder];
NSFetchRequest* request = [entityManager fetchRequestFromTemplateWithName:ActivityOffline substitutionVariables:variables];
Activity* activity = [entityManager entityForFetchRequest:request];
return activity;
}
但这只打印出最后一个值。我不确定如何打印满足条件/表达式的所有活动?
答案 0 :(得分:1)
获取请求模板应如下所示(变量用$表示(例如$ VARIABLE_NAME)):
从模型中检索必要的提取请求,如下所示:
NSFetchRequest* request = [self.managedObjectContext.persistentStoreCoordinator.managedObjectModel fetchRequestFromTemplateWithName:@"ActivityOffline" substitutionVariables:@{@"ANY_VARIABLE_NAME":offline}];