核心数据连接表,有许多通过,获取属性谓词 [英] Core data join table, has many through, fetched properties predicate
问题描述
如何为将要呈现的地方实体编写提取的财产一个用户数组?
How do you write a Fetched Property for the Place Entity that will present an Array of Users?
推荐答案
对于提取的属性用户
的 Place 检索所有签到与指定地点相关的用户,设置
For a fetched property users
of Place that retrieves all users whose check-in is related to the given place, set
- 获取的目的地属性用户
- 和谓词ANY checkins.event == $ FETCH_SOURCE
现在你可以获得一个地方的用户数组:
Now you can get the array of users for a place:
Place *place = ...;
NSArray *users = [place valueForKey:@"users"];
此获取的属性对应于以下获取请求:
This fetched property corresponds to the following fetch request:
Place *place = ...;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY checkins.event == %@", place];
[request setPredicate:predicate];
NSArray *users = [context executeFetchRequest:request error:&error];
如果您将获取的属性 users
声明为动态属性:
If you declare the fetched property users
as a dynamic property:
@interface Place (FetchedProperties)
@property(nonatomic, retain) NSArray *users;
@end
@implementation Place (FetchedProperties)
@dynamic users;
@end
然后您可以使用属性语法检索值:
then you can retrieve the value using property syntax:
NSArray *users = place.users;
// instead of: NSArray *users = [place valueForKey:@"users"];
但请注意 您可以获得直接相同的结果(作为一组),不使用fetched属性:
But note that you can get the same result (as a set) directly, without using a fetched property:
Place *place = ...;
NSSet *users = [place.checkins valueForKey:@"user"];
这篇关于核心数据连接表,有许多通过,获取属性谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!