在交易表中属性:
ispopular(attribute)
groupname(attribute)
dealsassets(relationship-name to assets)
在 Assets 表中,属性是:
assettype(attribute)
caption(attribute)
dealassetid(attribute)
assetsdeal(inverse relationshipname to deals)
交易是 Assets 一对多关系& Assets 与交易多对一关系
我想编写一个查询,我需要的是 ispopular == 1 然后是相关字段的 assets.dealasseti
,
我必须编写什么谓词查询,有人可以帮助我吗?
问候
最佳答案
这是您要找的吗?
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Assets"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"assetsdeal.ispopular == 1"];
[request setPredicate:predicate];
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
使用逆关系,您可以要求相关交易具有属性“ispopular == 1”的所有 Assets 。
替代解决方案(如果第一个解决方案由于某些 StackMob 限制而不起作用): 首先获取“ispopular == 1”的交易:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Deals"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ispopular == 1"];
[request setPredicate:predicate];
NSError *error;
NSArray *deals = [context executeFetchRequest:request error:&error];
并使用Key-Value Coding来获取相关 Assets :
NSArray *assets = [deals valueForKeyPath:@"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a4e4f4b46594b59594f5e59046a4e43595e4344495e7f44434544654c794f5e5904594f464c" rel="noreferrer noopener nofollow">[email protected]</a>"]
关于iphone - Core-Data:多对多关系谓词以获取多个相关表中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17235528/