0

i have two NSManagedObject classes, UserInfo and Department (to-one and to-many relationship respectively )here is my code,

// UserInfo.h

@interface UserInfo : NSManagedObject

@property (nonatomic, retain) NSDate * bDate;
@property (nonatomic, retain) NSString * firstName;
@property (nonatomic, retain) NSString * lastName;
@property (nonatomic, retain) NSString * userName;
@property (nonatomic, retain) Department *dept;

@end

//Department.h

@class UserInfo;

@interface Department : NSManagedObject

@property (nonatomic, retain) NSString * id;
@property (nonatomic, retain) NSString * post;
@property (nonatomic, retain) NSString * location;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *user;
@end

@interface Department (CoreDataGeneratedAccessors)

- (void)addUserObject:(UserInfo *)value;
- (void)removeUserObject:(UserInfo *)value;
- (void)addUser:(NSSet *)values;
- (void)removeUser:(NSSet *)values;
@end

for searching particular user from db i used, which works well

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstName contains[cd] %@", NameTosearch];

but, now what kind of predicate i can use to fetch all users with info connected to particular department?

BaSha
  • 2,356
  • 3
  • 21
  • 38

2 Answers2

1

You can also use a comparison with an object in predicates.

I.e. If you have a pointer to a Department instance in dept:

...withFormat:@"dept = %@", dept"

internally CoreData will replace the %@ with the managed object identifier of the object.

Cocoanetics
  • 8,171
  • 2
  • 30
  • 57
0

Search using

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstName contains[cd] == %@", NameTosearch];  

Follow this blog for more info http://rajneesh071.blogspot.in/
And follow my answer StackOverflow

Community
  • 1
  • 1
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74