I have three realm objects. Department, section and user. section is a kind of sub department in a department. But there will be users under each section and each department.
@interface Department : RLMObject @property NSString *name; @property BOOL isCollapsed; @property (nonatomic, strong) RLMArray *sections; @property (nonatomic, strong) RLMArray *users; @end @interface Section : RLMObject @property NSString *name; @property BOOL isCollapsed; @property RLMArray *users; @end @interface User : RLMObject @property NSString *department; @property NSString *email; @property NSString *firstname; @property NSString *lastname; @property NSString *fullname; @end What i want to do is, I want to search all the user whose first name and last name contains "a" and linked with department and section.
Now what i am doing is
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]; Right now the issue is there is two sections in sections array but in the deparments results, it returns null. please help. thanks.
Note: If one user already belongs to section then that user will not be include in department.