【发布时间】:2016-08-11 07:58:48
【问题描述】:
我有三个领域对象。部门、部门和用户。部门是部门中的一种子部门。但是每个部分和每个部门都会有用户。
@interface Department : RLMObject
@property NSString *name;
@property BOOL isCollapsed;
@property (nonatomic, strong) RLMArray<Section> *sections;
@property (nonatomic, strong) RLMArray<User> *users;
@end
@interface Section : RLMObject
@property NSString *name;
@property BOOL isCollapsed;
@property RLMArray<User> *users;
@end
@interface User : RLMObject
@property NSString *department;
@property NSString *email;
@property NSString *firstname;
@property NSString *lastname;
@property NSString *fullname;
@end
我想要做的是,我想搜索所有名字和姓氏包含“a”并与部门和部门链接的用户。
现在我正在做的是
searchText = [searchText lowercaseString];
RLMResults *sections = [Section objectsWhere:
[NSString stringWithFormat:@"SUBQUERY(users, $user, $user.fullname contains '%@' OR $user.nickname contains '%@').@count > 0",searchText,searchText]];
NSMutableArray *sectionNames = [NSMutableArray array];
for (Section *section in sections)
{
[sectionNames addObject:section.name];
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(sections, $section, $section.name IN %@).@count > 0", sectionNames];
RLMResults *filteredDepartments = [[Department objectsWithPredicate:predicate] sortedResultsUsingProperty:@"name" ascending:YES];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SUBQUERY(users, $user, $user.fullname contains %@ OR $user.nickname contains %@).@count > 0",searchText,searchText];
RLMResults *departments = [filteredDepartments objectsWithPredicate:pred];
现在的问题是 sections 数组中有两个部分,但在 deparments 结果中,它返回 null。请帮忙。谢谢。
注意:如果一个用户已经属于某个部门,那么该用户将不会被包含在部门中。
【问题讨论】:
-
过滤部门确实给了你结果吗?
-
是的。它返回两个部分。过滤的部门是正确的。 @user3182143
-
是因为 users.count == 0 在这个查询中吗? ` NSPredicate *pred = [NSPredicate predicateWithFormat:@"SUBQUERY(users, $user, $user.fullname contains %@ OR $user.nickname contains %@).@count > 0",searchText,searchText];`
标签: ios objective-c realm