I have two entities in core data with one to many relationship between them (Dream has one to many relationship with symbols). I have also provided search functionality by using predicate and the appropriate searchFetchedResultsController which is called when search is happening.
Now I want to implement search scope functionality using search bar scope buttons. How can I change my predicate so that it filters the content based on symbols rather than filtering on dream which is working fine. Do I need to create another fetchedResultsController or I should modify the predicate in this searchFetchedResultsController to get the desired results???
- (NSFetchedResultsController *)newFetchedResultsControllerWithSearch:(NSString *)searchString {
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dream"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"dateTimeStamp" ascending:NO] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Configure predicate
NSPredicate *filterPredicate = nil;
if (searchString.length) {
filterPredicate = [NSPredicate predicateWithFormat:@"note CONTAINS[cd] %@", searchString];
}
[fetchRequest setPredicate:filterPredicate];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
aFetchedResultsController.delegate = self;
NSError *error = nil;
if (![aFetchedResultsController performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return aFetchedResultsController;
}
No need for a separate fetched results controller.
The predicate could be something like this: