Skip to content

Instantly share code, notes, and snippets.

@mdziadkowiec
Created May 14, 2014 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdziadkowiec/e74a044fcd0d2cb2229c to your computer and use it in GitHub Desktop.
Save mdziadkowiec/e74a044fcd0d2cb2229c to your computer and use it in GitHub Desktop.
- (void)reloadFetchedResultsController {
[NSFetchedResultsController deleteCacheWithName:@"Customers"];
if (_request == nil) {
_request = [[NSFetchRequest alloc] initWithEntityName:@"Customer"];
}
NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
if (_departament) {
NSPredicate *query = [NSPredicate predicateWithFormat:@"departament = %@", _departament];
predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[ predicate, query ]];
}
if (_searchBar.text.length) {
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"lastname contains[cd] %@", _searchBar.text];
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"firstname contains[cd] %@", _searchBar.text];
NSPredicate *query = [NSCompoundPredicate orPredicateWithSubpredicates:@[pred1,pred2]];
predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[ predicate, query ]];
}
_request.predicate = predicate;
NSSortDescriptor *lastnameSort = [NSSortDescriptor sortDescriptorWithKey:_sortByKey
ascending:YES];
[_request setSortDescriptors:@[lastnameSort]];
if (_resultsCtl == nil) {
_resultsCtl = [[NSFetchedResultsController alloc] initWithFetchRequest:_request
managedObjectContext:self.moc
sectionNameKeyPath:@"sortLetter"
cacheName:@"Customers"];
_resultsCtl.delegate = self;
}
NSError *error = nil;
[_resultsCtl performFetch:&error];
if (error) {
NSLog(@"performFetch error = %@", error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment